📖 Scenario: Imagine you are organizing your personal notes in a notebook. Each note has a clear title so you can find it easily later. In programming, variables are like these notes. They need clear names so the computer and you understand what they hold.
🎯 Goal: You will create variables with proper names following PHP rules. Then, you will print their values to see the result.
📋 What You'll Learn
Create variables with valid PHP names
Assign simple values to these variables
Print the variables to show their values
💡 Why This Matters
🌍 Real World
Variable naming is important in all programming tasks to keep code clear and understandable.
💼 Career
Good variable names help developers read and maintain code, which is essential in any programming job.
Progress0 / 4 steps
1
Create variables with valid names
Create three variables called $name, $age, and $is_student. Assign the values "Alice", 23, and true to them respectively.
PHP
Hint
Remember, PHP variables start with a dollar sign $ followed by letters, numbers, or underscores. They cannot start with a number.
2
Add a variable for height
Create a variable called $height_cm and assign it the value 170.
PHP
Hint
Use underscores to separate words in variable names for readability.
3
Use variables in a message
Create a variable called $message that combines $name and $age into the string: "Alice is 23 years old." Use double quotes and variable interpolation.
PHP
Hint
Use double quotes to include variables directly inside strings in PHP.
4
Print the message
Use echo to print the variable $message.
PHP
Hint
Use echo to show text or variable values on the screen in PHP.
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.