Bird
Raised Fist0
PHPprogramming~10 mins

Type coercion in operations in PHP - Interactive Code Practice

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

Complete the code to add a string number and an integer.

PHP
<?php
$result = '5' [1] 3;
echo $result;
?>
Drag options to blanks, or click blank then click option'
A+
B.
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dot operator (.) which concatenates strings instead of adding numbers.
2fill in blank
medium

Complete the code to concatenate a string and a number.

PHP
<?php
$result = 'Value: ' [1] 10;
echo $result;
?>
Drag options to blanks, or click blank then click option'
A*
B+
C-
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using plus (+) which tries to add and converts the string to zero.
3fill in blank
hard

Fix the error in the code to correctly multiply a string number by an integer.

PHP
<?php
$result = '4' [1] 2;
echo $result;
?>
Drag options to blanks, or click blank then click option'
A.
B*
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dot operator (.) which causes a string concatenation instead of multiplication.
4fill in blank
hard

Fill both blanks to create an array where keys are string lengths and values are the strings themselves, only for strings longer than 3 characters.

PHP
<?php
$words = ['apple', 'cat', 'banana', 'dog'];
$result = array_combine(array_map('strlen', array_filter($words, function($word) {
    return strlen($word) [1] 3;
})), array_filter($words, function($word) {
    return strlen($word) [2] 3;
}));
print_r($result);
?>
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or == which would select wrong words or none.
5fill in blank
hard

Fill both blanks to create a dictionary with uppercase keys, original values, and only include values greater than 10.

PHP
<?php
$data = ['a' => 5, 'b' => 15, 'c' => 20];
$result = array_filter(array_combine(array_map('[1]', array_keys($data)), $data), function($value) {
    return $value [2] 10;
});
print_r($result);
?>
Drag options to blanks, or click blank then click option'
Astrtoupper
B>
C>=
Dstrtolower
Attempts:
3 left
💡 Hint
Common Mistakes
Using strtolower instead of strtoupper.
Using >= instead of > changes the filter condition.

Practice

(1/5)
1. In PHP, what happens when you add a string containing a number to an integer, like '5' + 3?
easy
A. PHP throws a type error because you can't add string and integer.
B. The result is the string '53' by concatenating '5' and '3'.
C. The string '5' is converted to the number 5, then added to 3, resulting in 8.
D. The operation returns null because of incompatible types.

Solution

  1. Step 1: Understand PHP type coercion in addition

    PHP automatically converts strings containing numbers to integers or floats when used in arithmetic operations.
  2. Step 2: Apply addition after conversion

    The string '5' becomes the integer 5, then 5 + 3 equals 8.
  3. Final Answer:

    8 -> Option C
  4. Quick Check:

    '5' + 3 = 8 [OK]
Hint: Strings with numbers convert to numbers in math operations [OK]
Common Mistakes:
  • Thinking PHP concatenates strings with + operator
  • Expecting a type error on mixed types
  • Assuming the result stays a string
2. Which of the following PHP expressions correctly adds a boolean true to an integer 2 and returns 3?
easy
A. (int)'true' + 2
B. true + 2
C. true . 2
D. 'true' + 2

Solution

  1. Step 1: Understand boolean to integer conversion

    In PHP, boolean true converts to integer 1 in arithmetic operations.
  2. Step 2: Evaluate each option

    (int)'true' + 2: (int)'true' is 0 + 2 = 2 (wrong). 'true' + 2: 'true' converts to 0 + 2 = 2 (wrong). true . 2: concatenation '12' (wrong). true + 2: 1 + 2 = 3 (correct).
  3. Final Answer:

    true + 2 -> Option B
  4. Quick Check:

    true + 2 = 3 [OK]
Hint: Boolean true acts as 1 in math operations [OK]
Common Mistakes:
  • Confusing string 'true' with boolean true
  • Using concatenation instead of addition
  • Not knowing boolean converts to 1 or 0
3. What is the output of the following PHP code?
$a = '10 apples';
$b = 5;
echo $a + $b;
medium
A. 105
B. 10 apples5
C. Error: Unsupported operand types
D. 15

Solution

  1. Step 1: Understand string to number conversion in arithmetic

    PHP converts the string '10 apples' to the number 10 by reading digits from the start until non-digit characters.
  2. Step 2: Perform addition

    10 + 5 equals 15, so echo outputs 15.
  3. Final Answer:

    15 -> Option D
  4. Quick Check:

    '10 apples' + 5 = 15 [OK]
Hint: Strings starting with numbers convert to those numbers in math [OK]
Common Mistakes:
  • Expecting string concatenation with +
  • Thinking it causes an error
  • Assuming the whole string converts to zero
4. Consider this PHP code snippet:
$x = '5 cats';
$y = 3;
$result = $x - $y;
echo $result;

Why might this code produce an unexpected result?
medium
A. PHP converts '5 cats' to 5, so 5 - 3 = 2, which might be unexpected if you expect string behavior.
B. Because subtraction is not defined for strings, it causes a fatal error.
C. The variable $result will be the string '5 cats3' due to concatenation.
D. PHP treats both as strings and outputs '5 cats3'.

Solution

  1. Step 1: Analyze subtraction with string and integer

    PHP converts the string '5 cats' to the number 5 for arithmetic operations.
  2. Step 2: Calculate the subtraction

    5 - 3 equals 2, so $result is 2, which may surprise if expecting string concatenation or error.
  3. Final Answer:

    PHP converts '5 cats' to 5, so result is 2 -> Option A
  4. Quick Check:

    '5 cats' - 3 = 2 [OK]
Hint: Subtraction forces string to number conversion [OK]
Common Mistakes:
  • Expecting string concatenation with - operator
  • Thinking it causes an error
  • Assuming result is a string
5. You want to compare two variables in PHP: $a = '0' (string) and $b = false (boolean). Which comparison returns true when using type coercion, and which returns false when checking type strictly?

Choose the correct pair:
hard
A. $a == $b is true; $a === $b is false
B. $a == $b is false; $a === $b is true
C. $a == $b is true; $a === $b is true
D. $a == $b is false; $a === $b is false

Solution

  1. Step 1: Understand loose (==) vs strict (===) comparison

    Loose comparison converts types before comparing; strict compares value and type exactly.
  2. Step 2: Evaluate comparisons

    Loose: '0' converts to 0, false converts to 0, so 0 == 0 is true. Strict: '0' is string, false is boolean, types differ, so false.
  3. Final Answer:

    $a == $b is true; $a === $b is false -> Option A
  4. Quick Check:

    '0' == false is true; '0' === false is false [OK]
Hint: Use === to check type and value, == allows type coercion [OK]
Common Mistakes:
  • Assuming == checks type strictly
  • Thinking '0' and false are always different
  • Confusing loose and strict equality
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