← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 8 of 13

PHP Keywords और Identifiers

Keywords वे special words हैं जो PHP पहले से अपने काम के लिए इस्तेमाल करता है, किसी theater में reserved seats जैसे। Identifiers वे names हैं जो आप खुद अपनी चीज़ों के लिए चुनते हैं, बशर्ते आप कोई reserved seat न लें।
Syntax
php
$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
<?php
if (true) {
    echo "if, else, function, class are reserved keywords";
}
// $if = 5; would cause a parse error -- keywords can't be variable names
?>

Identifiers

एक identifier कोई भी name है जो *आप* चुनते हैं — किसी variable, function, या class के लिए — ताकि PHP को पता चले कि आपने जो चीज़ बनाई है उसे क्या कहना है।

$totalPrice जैसा एक clear identifier अपने आप intent document कर देता है, पढ़ने वाले को यह guess करने से बचाते हुए कि $x जैसा एक vaguer name किस के लिए है।

उदाहरण: Identifiers

php
<?php
$totalPrice = 49.99;
echo $totalPrice;
// A clear name documents intent better than $x would
?>

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
<?php
$user_name = "Alice";
echo $user_name;
// $user-name would fail to parse -- hyphens aren't allowed in identifiers
?>

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
<?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";
?>

Names के लिए Best Practices

ऐसे names पसंद करें जो बताएँ कि value क्या represent करती है — $e के बजाय $userEmail — खासकर उनके लिए जिन्हें आप एक बार से ज़्यादा reference करेंगे।

Extra characters runtime पर कुछ भी cost नहीं करते, और वे बाद में code पढ़ने वाले किसी के भी (future आप सहित) असली समय बचाते हैं।

उदाहरण: Best Practices for Names

php
<?php
$userEmail = "[email protected]";
echo $userEmail;
// Preferred over a vague name like $e
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी variable name को एक digit से शुरू करना, जैसे $1st_place, जो एक parse error है क्योंकि names को एक letter या underscore से शुरू होना चाहिए।
  2. class या function जैसे reserved keyword को constant या function name की तरह इस्तेमाल करना, जो एक syntax error produce करता है।
  3. $user-name को hyphen के साथ इस्तेमाल करना, जिसे PHP $user - name (subtraction) की तरह पढ़ता है, एक variable name की तरह नहीं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.