0
0
PHPprogramming~10 mins

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

Choose your learning style9 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.