PHP Keywords और Identifiers
In this page:
$identifier = value; // identifier: letter or _ first, then letters, digits, _
function identifier() { }
class Identifier { }
Keywords
Keywords वे words हैं जिनके लिए PHP ने पहले से एक specific meaning claim कर रखी है — if, else, function, class, और दर्जनों और।
क्योंकि interpreter program flow control करने के लिए ये words इस्तेमाल करता है, आप इनमें से किसी को variable या function name की तरह reuse नहीं कर सकते; ऐसा करने से एक parse error होता है।
उदाहरण: Keywords
<?php
if (true) {
echo "if, else, function, class are reserved keywords";
}
// $if = 5; would cause a parse error -- keywords can't be variable names
?>
Login to try C/C++/Java/PHP code in the editor
Identifiers
एक identifier कोई भी name है जो *आप* चुनते हैं — किसी variable, function, या class के लिए — ताकि PHP को पता चले कि आपने जो चीज़ बनाई है उसे क्या कहना है।
$totalPrice जैसा एक clear identifier अपने आप intent document कर देता है, पढ़ने वाले को यह guess करने से बचाते हुए कि $x जैसा एक vaguer name किस के लिए है।
उदाहरण: Identifiers
<?php
$totalPrice = 49.99;
echo $totalPrice;
// A clear name documents intent better than $x would
?>
Login to try C/C++/Java/PHP code in the editor
Naming Rules
PHP को चाहिए कि identifiers एक letter या underscore से शुरू हों, और उसके बाद सिर्फ letters, digits, और underscores हों — कोई spaces, hyphens, या special symbols नहीं।
यह एक hard syntax rule है, style preference नहीं: $user-name एक single identifier की तरह parse नहीं हो पाएगा।
उदाहरण: Naming Rules
<?php
$user_name = "Alice";
echo $user_name;
// $user-name would fail to parse -- hyphens aren't allowed in identifiers
?>
Login to try C/C++/Java/PHP code in the editor
Names में Case Sensitivity
PHP में variable names case-sensitive हैं, इसलिए $page, $Page, और $PAGE तीन पूरी तरह अलग variables हैं जो कोई value share नहीं करते।
यह beginners को language के किसी भी दूसरे casing rule से ज़्यादा confuse करता है, क्योंकि function और keyword names इस तरह behave *नहीं* करते।
उदाहरण: Case Sensitivity in Names
<?php
// Declare `$page`, set to "home"
$page = "home";
// Declare `$Page`, set to "about"
$Page = "about";
// Declare `$PAGE`, set to "contact"
$PAGE = "contact";
// Print "$page, $Page, $PAGE are three separate variables" to the output
echo "$page, $Page, $PAGE are three separate variables";
?>
Login to try C/C++/Java/PHP code in the editor
Names के लिए Best Practices
ऐसे names पसंद करें जो बताएँ कि value क्या represent करती है — $e के बजाय $userEmail — खासकर उनके लिए जिन्हें आप एक बार से ज़्यादा reference करेंगे।
Extra characters runtime पर कुछ भी cost नहीं करते, और वे बाद में code पढ़ने वाले किसी के भी (future आप सहित) असली समय बचाते हैं।
उदाहरण: Best Practices for Names
<?php
$userEmail = "[email protected]";
echo $userEmail;
// Preferred over a vague name like $e
?>
Login to try C/C++/Java/PHP code in the editor
- किसी variable name को एक digit से शुरू करना, जैसे
$1st_place, जो एक parse error है क्योंकि names को एक letter या underscore से शुरू होना चाहिए। classयाfunctionजैसे reserved keyword को constant या function name की तरह इस्तेमाल करना, जो एक syntax error produce करता है।$user-nameको hyphen के साथ इस्तेमाल करना, जिसे PHP$user - name(subtraction) की तरह पढ़ता है, एक variable name की तरह नहीं।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: