Bird
0
0

You want to store a user's first name and age in PHP variables and then display them. Which code correctly declares and uses these variables?

hard📝 Application Q15 of 15
PHP - Variables and Data Types
You want to store a user's first name and age in PHP variables and then display them. Which code correctly declares and uses these variables?
1. $firstName = "Alice";
2. $age = 30;
3. echo "Name: $firstName, Age: $age";
ADeclare variables without $ sign and use $ in echo
BUse variables without $ sign in echo: echo "Name: firstName, Age: age";
CDeclare variables with $ and use them with $ in echo as shown
DUse $ only in declaration, not in echo
Step-by-Step Solution
Solution:
  1. Step 1: Declare variables with $ sign

    Lines 1 and 2 correctly declare variables $firstName and $age with $ sign.
  2. Step 2: Use variables with $ sign inside double quotes

    In PHP, variables inside double quotes are parsed and replaced with their values, so echo "Name: $firstName, Age: $age"; works correctly.
  3. Final Answer:

    Declare variables with $ and use them with $ in echo as shown -> Option C
  4. Quick Check:

    Variables need $ in declaration and usage [OK]
Quick Trick: Use $ sign in both declaration and usage [OK]
Common Mistakes:
  • Omitting $ sign in echo
  • Using single quotes which don't parse variables
  • Declaring variables without $ sign

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes