Bird
Raised Fist0
PHPprogramming~10 mins

Why variables are needed in PHP - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a variable in PHP.

PHP
<?php
$[1] = 10;
echo $number;
?>
Drag options to blanks, or click blank then click option'
Anumber
B10
Cecho
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number as a variable name
Forgetting the dollar sign before the variable name
Using reserved words like 'var' as variable names
2fill in blank
medium

Complete the code to output the value of the variable.

PHP
<?php
$age = 25;
echo [1];
?>
Drag options to blanks, or click blank then click option'
Aecho
B$25
C$age
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the dollar sign when echoing the variable
Trying to echo the variable name as a string without the dollar sign
3fill in blank
hard

Fix the error in the code by completing the variable assignment.

PHP
<?php
$[1] = 'Hello World';
echo $greeting;
?>
Drag options to blanks, or click blank then click option'
Aworld
Bmessage
CHello
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in assignment and output
Forgetting the dollar sign in variable names
4fill in blank
hard

Fill both blanks to create and output a variable holding a string.

PHP
<?php
$[1] = [2];
echo $name;
?>
Drag options to blanks, or click blank then click option'
Aname
B"John"
CJohn
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around string values
Using a variable name that doesn't match the echo statement
5fill in blank
hard

Fill all three blanks to declare, assign, and output a variable with a number.

PHP
<?php
$[1] = [2];
echo $[3];
?>
Drag options to blanks, or click blank then click option'
Ascore
B100
Dpoints
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in assignment and echo
Putting quotes around numbers

Practice

(1/5)
1. Why do we need variables in PHP?
easy
A. To create new functions automatically
B. To make the program run faster
C. To store and reuse data easily throughout the program
D. To change the PHP version

Solution

  1. Step 1: Understand the role of variables

    Variables hold data that can be used and changed later in the program.
  2. Step 2: Identify why this is useful

    Storing data in variables allows easy reuse and modification without rewriting values.
  3. Final Answer:

    To store and reuse data easily throughout the program -> Option C
  4. Quick Check:

    Variables store data for reuse [OK]
Hint: Variables hold data to use later in code [OK]
Common Mistakes:
  • Thinking variables speed up the program
  • Confusing variables with functions
  • Believing variables change PHP version
2. Which of the following is the correct way to declare a variable in PHP?
easy
A. name = "John";
B. $name = "John";
C. var name = "John";
D. let $name = "John";

Solution

  1. Step 1: Recall PHP variable syntax

    In PHP, variables start with a dollar sign ($) followed by the name.
  2. Step 2: Check each option

    Only $name = "John"; uses $ before the variable name and assigns a string correctly.
  3. Final Answer:

    $name = "John"; -> Option B
  4. Quick Check:

    PHP variables start with $ [OK]
Hint: PHP variables always start with $ [OK]
Common Mistakes:
  • Omitting the $ sign before variable name
  • Using JavaScript syntax like var or let
  • Assigning without semicolon
3. What will be the output of this PHP code?
$age = 25;
$age = $age + 5;
echo $age;
medium
A. 25
B. Error
C. 5
D. 30

Solution

  1. Step 1: Understand variable assignment and update

    $age starts at 25, then 5 is added and stored back in $age.
  2. Step 2: Calculate the new value

    25 + 5 = 30, so $age becomes 30.
  3. Final Answer:

    30 -> Option D
  4. Quick Check:

    25 + 5 = 30 [OK]
Hint: Adding to variable updates its value [OK]
Common Mistakes:
  • Expecting original value without addition
  • Confusing assignment with comparison
  • Thinking echo prints variable name
4. Find the error in this PHP code:
$count = 10
echo $count;
medium
A. Missing semicolon after variable assignment
B. Variable name should not start with $
C. echo cannot print variables
D. Variable must be declared with var keyword

Solution

  1. Step 1: Check syntax rules for PHP statements

    Each statement must end with a semicolon (;).
  2. Step 2: Identify missing semicolon

    The first line lacks a semicolon after 10, causing a syntax error.
  3. Final Answer:

    Missing semicolon after variable assignment -> Option A
  4. Quick Check:

    Statements need ; at end [OK]
Hint: Always end PHP statements with ; [OK]
Common Mistakes:
  • Forgetting semicolon at line end
  • Thinking $ is not needed for variables
  • Believing echo can't print variables
5. You want to store a user's first name and last name separately, then combine them into a full name variable. Which PHP code correctly does this?
hard
A. $first = "Anna"; $last = "Lee"; $full = $first . " " . $last; echo $full;
B. $first = "Anna"; $last = "Lee"; $full = $first + " " + $last; echo $full;
C. $first = "Anna"; $last = "Lee"; $full = '$first $last'; echo $full;
D. $first = "Anna"; $last = "Lee"; $full = $first & " " & $last; echo $full;

Solution

  1. Step 1: Understand string concatenation in PHP

    PHP uses the dot (.) operator to join strings.
  2. Step 2: Check each option for correct concatenation

    $first = "Anna"; $last = "Lee"; $full = $first . " " . $last; echo $full; uses . to join first name, space, and last name correctly.
  3. Final Answer:

    $first = "Anna"; $last = "Lee"; $full = $first . " " . $last; echo $full; -> Option A
  4. Quick Check:

    Use . to join strings in PHP [OK]
Hint: Use . to join strings in PHP [OK]
Common Mistakes:
  • Using + or & instead of . for strings
  • Forgetting spaces between names
  • Trying to combine variables without concatenation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard