PHP Keywords & Identifiers
Keywords
Keywords are words PHP has already claimed a specific meaning for — if, else, function, class, and dozens more. Because the interpreter uses these words to control program flow, you can't reuse one as a variable or function name; doing so causes a parse error.
Example: 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
An identifier is any name *you* choose — for a variable, a function, or a class — so PHP knows what to call the thing you've created. A clear identifier like $totalPrice documents intent on its own, saving a reader from having to guess what a vaguer name like $x is supposed to hold.
Example: 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 requires identifiers to start with a letter or an underscore, and only letters, digits, and underscores after that — no spaces, hyphens, or special symbols. This is a hard syntax rule, not a style preference: $user-name will fail to parse as a single identifier.
Example: 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
Case Sensitivity in Names
Variable names are case-sensitive in PHP, so $page, $Page, and $PAGE are three entirely separate variables that don't share a value. This trips up beginners more than any other casing rule in the language, since function and keyword names *don't* behave this way.
Example: Case Sensitivity in Names
<?php
$page = "home";
$Page = "about";
$PAGE = "contact";
echo "$page, $Page, $PAGE are three separate variables";
?>
Login to try C/C++/Java/PHP code in the editor
Best Practices for Names
Favor names that describe what a value represents — $userEmail over $e — especially for anything you'll reference more than once. The extra characters cost nothing at runtime, and they save real time for whoever (including future you) has to read the code later.
Example: 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
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: