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

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

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
<?php
$totalPrice = 49.99;
echo $totalPrice;
// A clear name documents intent better than $x would
?>

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

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
<?php
$page = "home";
$Page = "about";
$PAGE = "contact";
echo "$page, $Page, $PAGE are three separate variables";
?>

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
<?php
$userEmail = "[email protected]";
echo $userEmail;
// Preferred over a vague name like $e
?>

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.