Bird
0
0

Which variable name is invalid in PHP and why?

hard📝 Application Q9 of 15
PHP - Variables and Data Types
Which variable name is invalid in PHP and why?
$user_name
$UserName
$userName1
$user-name
A$UserName because uppercase letters are not allowed
B$user_name because underscores are not allowed
C$userName1 because numbers are not allowed
D$user-name because hyphens are not allowed
Step-by-Step Solution
Solution:
  1. Step 1: Review allowed characters in PHP variable names

    Letters, numbers, and underscores are allowed; hyphens are not.
  2. Step 2: Check each variable

    $user-name contains a hyphen, which is invalid. Others are valid.
  3. Final Answer:

    $user-name is invalid because hyphens are not allowed. -> Option D
  4. Quick Check:

    Hyphens not allowed in variable names = B [OK]
Quick Trick: Hyphens break variable names; use underscores instead [OK]
Common Mistakes:
  • Using hyphens in variable names
  • Thinking uppercase letters are invalid
  • Avoiding numbers at the end

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes