Bird
Raised Fist0
PHPprogramming~10 mins

Variable naming rules in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Variable naming rules
Start
↓
Check first character
↓
Check rest characters
↓
Valid variable name
↓
Use variable
↓
Error: invalid characters
↓
Error: invalid first character
Variable names must start with a letter or underscore, followed by letters, digits, or underscores only.
Execution Sample
PHP
<?php
$var1 = 10;
$_var2 = 20;
$3var = 30; // invalid
$var_3 = 40;
?>
This code shows valid and invalid variable names in PHP.
Execution Table
StepVariable NameCheck First CharacterCheck Remaining CharactersResult
1$var1v (letter) - valida,r,1 (letters/digit) - validValid variable name
2$_var2_ (underscore) - validv,a,r,2 (letters/digit) - validValid variable name
3$3var3 (digit) - invalidN/AError: invalid first character
4$var_3v (letter) - valida,r,_,3 (letters/digit/underscore) - validValid variable name
💡 Step 3 stops due to invalid first character '3' in variable name.
Variable Tracker
Variable NameStartAfter Check First CharacterAfter Check Remaining CharactersFinal Result
$var1Not checkedValidValidValid variable name
$_var2Not checkedValidValidValid variable name
$3varNot checkedInvalidN/AError: invalid first character
$var_3Not checkedValidValidValid variable name
Key Moments - 3 Insights
Why can't a variable name start with a digit like $3var?
Because PHP requires the first character to be a letter or underscore, as shown in execution_table step 3 where '3' is invalid.
Are digits allowed anywhere in the variable name?
Yes, digits can appear after the first character, as shown in $var1 and $var_3 in execution_table steps 1 and 4.
Is underscore allowed at the start of a variable name?
Yes, underscore is allowed as the first character, as shown in $_var2 in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of checking the variable name $3var at step 3?
AError: invalid first character
BValid variable name
CError: invalid characters in the middle
DValid but discouraged
💡 Hint
Refer to execution_table row 3 under 'Result' column.
At which step does the variable name start with an underscore?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Check 'Check First Character' column in execution_table.
If we change $var_3 to $var-3, what would happen in the checks?
AError: invalid first character
BError: invalid characters in the middle
CIt would be valid
DNo error, just a warning
💡 Hint
Hyphen '-' is not allowed in variable names, see rules in concept_flow.
Concept Snapshot
PHP variable names must start with a letter or underscore (_).
After the first character, letters, digits, and underscores are allowed.
Variable names cannot start with digits or contain special characters.
Examples: $var1, $_var2, $var_3 are valid; $3var is invalid.
Full Transcript
In PHP, variable names must start with a letter or underscore. The rest of the name can include letters, digits, or underscores. For example, $var1 and $_var2 are valid variable names. However, $3var is invalid because it starts with a digit. This rule helps PHP understand variable names correctly and avoid errors. When checking variable names, first the initial character is verified, then the rest are checked for allowed characters. If any character is invalid, PHP will raise an error. Remember, no special characters like hyphens or spaces are allowed in variable names.

Practice

(1/5)
1. Which of the following is a valid variable name in PHP?
easy
A. $user_name
B. 2ndUser
C. user-name
D. user name

Solution

  1. Step 1: Check the first character of each variable

    PHP variables must start with a $ followed by a letter or underscore, not a number or space.
  2. Step 2: Validate allowed characters

    Variable names can only contain letters, numbers, and underscores after the $. Spaces and hyphens are not allowed.
  3. Final Answer:

    $user_name -> Option A
  4. Quick Check:

    Starts with $ and uses letters/underscore [OK]
Hint: Variable names start with $ and no spaces or hyphens [OK]
Common Mistakes:
  • Starting variable with a number
  • Using spaces or hyphens in variable names
  • Omitting the $ sign at the start
2. Which of these variable declarations will cause a syntax error in PHP?
easy
A. $totalAmount = 100;
B. $_count = 5;
C. $9value = 10;
D. $userName = 'John';

Solution

  1. Step 1: Identify the variable starting characters

    PHP variables must start with $ followed by a letter or underscore, not a number.
  2. Step 2: Check each option

    Options B, C, and D start correctly. $9value = 10; starts with $9, which is invalid.
  3. Final Answer:

    Syntax error due to starting with a number -> Option C
  4. Quick Check:

    Variable cannot start with number [OK]
Hint: Variables cannot start with numbers, only letters or _ after $ [OK]
Common Mistakes:
  • Starting variable names with digits
  • Confusing variable names with constants
  • Missing the $ sign
3. What will be the output of the following PHP code?
$Var = 10;
$var = 20;
echo $Var + $var;
medium
A. 20
B. 30
C. 10
D. Error

Solution

  1. Step 1: Understand variable case sensitivity in PHP

    PHP variable names are case-sensitive, so $Var and $var are different variables.
  2. Step 2: Calculate the sum of both variables

    $Var is 10 and $var is 20, so their sum is 30.
  3. Final Answer:

    30 -> Option B
  4. Quick Check:

    Case-sensitive variables sum correctly [OK]
Hint: PHP variables are case-sensitive; $Var and $var differ [OK]
Common Mistakes:
  • Assuming variables with different cases are the same
  • Expecting a syntax error due to case difference
  • Ignoring case sensitivity in variable names
4. Identify the error in this PHP code:
$1stName = 'Alice';
$lastName = 'Smith';
echo $1stName . ' ' . $lastName;
medium
A. Variable names cannot contain uppercase letters
B. Missing semicolon after echo statement
C. Concatenation operator is incorrect
D. Variable names cannot start with a number

Solution

  1. Step 1: Check variable naming rules

    Variable names cannot start with a number. $1stName starts with '1', which is invalid.
  2. Step 2: Verify other parts of the code

    The concatenation operator . and semicolons are correct, and uppercase letters are allowed.
  3. Final Answer:

    Variable names cannot start with a number -> Option D
  4. Quick Check:

    Variable names must start with letter or _ [OK]
Hint: Variable names cannot start with digits, only letters or underscore [OK]
Common Mistakes:
  • Thinking uppercase letters are invalid
  • Confusing concatenation operator with plus sign
  • Missing semicolons causing errors
5. You want to store a user's age in a variable. Which variable name follows PHP naming rules and is the clearest choice?
hard
A. $userAge
B. $user-age
C. $age user
D. $2age

Solution

  1. Step 1: Check PHP variable naming rules

    Variable names must start with $ followed by a letter or underscore, and can contain letters, numbers, and underscores only.
  2. Step 2: Evaluate clarity and correctness of options

    $userAge uses camelCase with allowed characters. Options A and B contain hyphens and spaces, which are invalid. $2age starts with a number, which is invalid.
  3. Final Answer:

    $userAge -> Option A
  4. Quick Check:

    Clear, valid variable name with letters and no invalid characters [OK]
Hint: Use letters and underscores, no spaces or hyphens, start with $ [OK]
Common Mistakes:
  • Using spaces or hyphens in variable names
  • Starting variable names with numbers
  • Choosing unclear or confusing names
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard