Bird
Raised Fist0
PHPprogramming~10 mins

Why operators matter 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 add two numbers and print the result.

PHP
<?php
$a = 5;
$b = 3;
$result = $a [1] $b;
echo $result;
?>
Drag options to blanks, or click blank then click option'
A-
B*
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
2fill in blank
medium

Complete the code to check if two values are equal.

PHP
<?php
$x = 10;
$y = 10;
if ($x [1] $y) {
    echo 'Equal';
} else {
    echo 'Not equal';
}
?>
Drag options to blanks, or click blank then click option'
A==
B!=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment = instead of comparison ==.
3fill in blank
hard

Fix the error in the code to multiply two numbers correctly.

PHP
<?php
$num1 = 4;
$num2 = 7;
$product = $num1 [1] $num2;
echo $product;
?>
Drag options to blanks, or click blank then click option'
A-
B*
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or division instead of multiplication.
4fill in blank
hard

Fill both blanks to create an array of squares for numbers greater than 3.

PHP
<?php
$numbers = [1, 2, 3, 4, 5];
$squares = array_filter(array_map(fn($n) => $n[1]2, $numbers), fn($n) => $n [2] 9);
print_r($squares);
?>
Drag options to blanks, or click blank then click option'
A**
B>
C<
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of power, or wrong comparison operator.
5fill in blank
hard

Fill all three blanks to create an associative array with keys as uppercase letters and values greater than 2.

PHP
<?php
$items = ['a' => 1, 'b' => 3, 'c' => 5];
$result = array_filter(array_combine(array_map(fn($k) => [1]($k), array_keys($items)), $items), fn($v) => $v [2] 2 && $v [3] 6);
print_r($result);
?>
Drag options to blanks, or click blank then click option'
Astrtoupper
B>
C<
Dstrtolower
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase function or wrong comparison operators.

Practice

(1/5)
1. What does the == operator do in PHP?
easy
A. Checks if two values are identical in type and value
B. Assigns a value to a variable
C. Adds two numbers together
D. Checks if two values are equal in value

Solution

  1. Step 1: Understand the == operator

    The == operator compares two values to see if they are equal, ignoring type differences.
  2. Step 2: Differentiate from other operators

    The = operator assigns values, + adds numbers, and === checks both value and type.
  3. Final Answer:

    Checks if two values are equal in value -> Option D
  4. Quick Check:

    == means equal value [OK]
Hint: Remember: == compares values, not types [OK]
Common Mistakes:
  • Confusing == with = (assignment)
  • Thinking == checks type too
  • Mixing == with ===
2. Which of the following is the correct way to increment a variable $count by 1 in PHP?
easy
A. $count += 1;
B. $count = 1+;
C. $count =+ 1;
D. $count ++=;

Solution

  1. Step 1: Identify the correct increment syntax

    The operator += adds a value to the variable and assigns the result back.
  2. Step 2: Check each option's syntax

    $count =+ 1; uses =+ which is invalid. $count = 1+; has misplaced plus sign. $count ++=; is invalid syntax. Only $count += 1; is correct.
  3. Final Answer:

    $count += 1; -> Option A
  4. Quick Check:

    Use += to add and assign [OK]
Hint: Use += to add and assign in one step [OK]
Common Mistakes:
  • Writing =+ instead of += causes errors
  • Misplacing the plus sign
  • Using invalid syntax like ++=
3. What will be the output of this PHP code?
$a = 5;
$b = 10;
$result = $a > $b ? 'Yes' : 'No';
echo $result;
medium
A. 5
B. Yes
C. No
D. 10

Solution

  1. Step 1: Evaluate the comparison $a > $b

    Since 5 is not greater than 10, the condition is false.
  2. Step 2: Apply the ternary operator

    Because the condition is false, the expression returns 'No'.
  3. Final Answer:

    No -> Option C
  4. Quick Check:

    5 > 10 is false, so output is 'No' [OK]
Hint: Ternary returns first if true, else second [OK]
Common Mistakes:
  • Assuming 5 > 10 is true
  • Confusing ternary syntax
  • Expecting numeric output instead of string
4. Find the error in this PHP code snippet:
$x = 10;
if ($x = 5) {
  echo 'Equal';
} else {
  echo 'Not Equal';
}
medium
A. The variable $x is not declared before use
B. The assignment operator '=' is used instead of comparison '==' in the if condition
C. The echo statements are missing semicolons
D. The else block is not allowed after if

Solution

  1. Step 1: Check the if condition operator

    The code uses = which assigns 5 to $x instead of comparing.
  2. Step 2: Understand the effect of assignment in condition

    Assignment returns the assigned value (5), which is truthy, so 'Equal' always prints regardless of original $x.
  3. Final Answer:

    The assignment operator '=' is used instead of comparison '==' in the if condition -> Option B
  4. Quick Check:

    Use == to compare, not = [OK]
Hint: Use == for comparison, = is assignment [OK]
Common Mistakes:
  • Using = instead of == in conditions
  • Thinking assignment returns boolean
  • Ignoring that assignment inside if is valid but wrong logic
5. You want to check if a variable $value is either 0 or an empty string '' in PHP. Which operator should you use to correctly test this condition?
hard
A. Use $value === 0 || $value === ''
B. Use $value == 0 || $value == ''
C. Use $value = 0 || $value = ''
D. Use $value != 0 && $value != ''

Solution

  1. Step 1: Understand the difference between == and ===

    == compares values loosely, === compares value and type strictly.
  2. Step 2: Choose strict comparison for exact match

    To check if $value is exactly 0 (integer) or exactly empty string, use === to avoid false matches.
  3. Step 3: Avoid assignment in condition

    Use $value = 0 || $value = '' uses = which assigns values, causing errors.
  4. Final Answer:

    Use $value === 0 || $value === '' -> Option A
  5. Quick Check:

    Use === for exact type and value checks [OK]
Hint: Use === to check both type and value exactly [OK]
Common Mistakes:
  • Using = instead of == or ===
  • Using == which allows type juggling
  • Using && instead of || for this check
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