What character must a PHP variable name start with?
A PHP variable name must start with a dollar sign ($). For example, $variable is valid.
Click to reveal answer
beginner
Can PHP variable names contain spaces or special characters like @ or #?
No, PHP variable names cannot contain spaces or special characters like @ or #. They can only contain letters, numbers, and underscores after the initial $.
Click to reveal answer
beginner
Are PHP variable names case-sensitive?
Yes, PHP variable names are case-sensitive. For example, $Name and $name are two different variables.
Click to reveal answer
beginner
Is $2ndValue a valid PHP variable name? Why or why not?
No, $2ndValue is not valid because variable names cannot start with a number after the dollar sign. The first character after $ must be a letter or underscore.
Click to reveal answer
beginner
What characters are allowed in PHP variable names after the initial $?
After the initial $, PHP variable names can contain letters (a-z, A-Z), numbers (0-9), and underscores (_). They cannot start with a number.
Click to reveal answer
Which of the following is a valid PHP variable name?
A$user_name
BuserName
C$9lives
D$user-name
✗ Incorrect
Only option C is valid. It starts with $ followed by a letter or underscore, and uses only allowed characters (letters, numbers, and underscore). A starts with a number after $, B lacks $, and D contains a hyphen.
Are PHP variable names case-sensitive?
AOnly in some PHP versions
BNo, $Var and $var are the same variable
CYes, $Var and $var are different variables
DOnly for variables starting with underscore
✗ Incorrect
PHP variable names are case-sensitive, so $Var and $var refer to different variables.
What is the first character of every PHP variable name?
A$
B_
Ca letter
Da number
✗ Incorrect
Every PHP variable name must start with a dollar sign ($).
Which character is NOT allowed in PHP variable names after the $?
Aa
B9
C_
D-
✗ Incorrect
Hyphens (-) are not allowed in PHP variable names.
Is this variable name valid in PHP? $_myVar123
ANo, because it contains uppercase letters
BYes
CNo, because it contains numbers
DNo, because it starts with underscore
✗ Incorrect
Variable names can start with an underscore and contain numbers and uppercase letters.
Explain the rules for naming variables in PHP.
Think about what characters are allowed and the importance of the $ sign.
You got /5 concepts.
Why is $2ndValue not a valid variable name in PHP? What would be a valid alternative?
Focus on the first character after the dollar sign.
You got /2 concepts.
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
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.
Step 2: Validate allowed characters
Variable names can only contain letters, numbers, and underscores after the $. Spaces and hyphens are not allowed.
Final Answer:
$user_name -> Option A
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
Step 1: Identify the variable starting characters
PHP variables must start with $ followed by a letter or underscore, not a number.
Step 2: Check each option
Options B, C, and D start correctly. $9value = 10; starts with $9, which is invalid.
Final Answer:
Syntax error due to starting with a number -> Option C
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
Step 1: Understand variable case sensitivity in PHP
PHP variable names are case-sensitive, so $Var and $var are different variables.
Step 2: Calculate the sum of both variables
$Var is 10 and $var is 20, so their sum is 30.
Final Answer:
30 -> Option B
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
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
Step 1: Check variable naming rules
Variable names cannot start with a number. $1stName starts with '1', which is invalid.
Step 2: Verify other parts of the code
The concatenation operator . and semicolons are correct, and uppercase letters are allowed.
Final Answer:
Variable names cannot start with a number -> Option D
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
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.
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.
Final Answer:
$userAge -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.