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

PHP Keywords & Identifiers

Keywords are special words PHP already uses for its own jobs, like reserved seats in a theater. Identifiers are the names you pick yourself for your own things, as long as you do not take a reserved seat.
Syntax
php
$identifier = value;   // identifier: letter or _ first, then letters, digits, _
function identifier() { }
class Identifier { }

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

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
?>
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. #}
Common Mistakes
  1. Starting a variable name with a digit, such as $1st_place, which is a parse error because names must begin with a letter or underscore.
  2. Using a reserved keyword like class or function as a constant or function name, which produces a syntax error.
  3. Using $user-name with a hyphen, which PHP reads as $user - name (subtraction), not one 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.