Skip to content

PHP Tutorial Variables and Constants

Variable Naming Rules

Lesson 22 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

PHP has a few strict rules for variable names, and a few habits that make your code much easier to read.

The rules

  1. A name starts with $.
  2. After the $, the first character must be a letter or an underscore.
  3. The rest can be letters, numbers or underscores.
  4. No spaces and no dashes.
  5. Names are case-sensitive. $total and $Total are different.
index.php
PHP
<?php
$name = "ok";
$_private = "ok";
$user2 = "ok";
$first_name = "ok";

echo "All valid";
Output
OUTPUT
All valid

These are not valid:

index.php
PHP
<?php
$2fast = 1;      // cannot start with a number
$first-name = 1; // a dash is the minus sign
$my name = 1;    // spaces are not allowed

Reserved words

You cannot use $this as a normal variable name. Other PHP keywords such as class or function are fine after the dollar sign ($class works), but avoid them because they confuse readers.

Naming style

PHP does not force a style, but most projects use:

ThingStyleExample
VariablescamelCase$userName
FunctionscamelCasegetUserName()
ClassesPascalCaseUserProfile
ConstantsUPPER_SNAKE_CASEMAX_LOGIN_ATTEMPTS

Some older code (and WordPress) writes variables as $user_name. Both are fine. Pick one style for a project and keep it.

Choose names that explain themselves

index.php
PHP
// Hard to read
$d = 86400;
$x = "Maya";

// Easy to read
$secondsPerDay = 86400;
$customerName = "Maya";

A good name removes the need for a comment. Avoid one-letter names except for tiny loops such as $i.

lightbulb
Names that start with a verb are for functions
Use nouns for variables ($total, $userList) and verbs for functions (calculateTotal()). It makes code read like a sentence.