Bird
Raised Fist0
PHPprogramming~10 mins

Type juggling 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 convert the string to an integer.

PHP
<?php
$value = "123";
$number = ([1])$value;
echo gettype($number); // outputs 'integer'
?>
Drag options to blanks, or click blank then click option'
Aint
Bstring
Cbool
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using (string) instead of (int) will keep the value as a string.
Using (bool) will convert to true or false, not a number.
2fill in blank
medium

Complete the code to check if the string '10 apples' equals the number 10 using ==.

PHP
<?php
$result = ('10 apples' [1] 10);
echo $result ? 'true' : 'false';
?>
Drag options to blanks, or click blank then click option'
A!==
B===
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using === will return false because types differ.
Using != or !== will check for inequality, not equality.
3fill in blank
hard

Fix the error in the code to correctly add a string number and an integer.

PHP
<?php
$a = "5";
$b = 3;
$sum = $a [1] $b;
echo $sum; // should output 8
?>
Drag options to blanks, or click blank then click option'
A*
B.
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using . will concatenate strings, resulting in '53' instead of 8.
Using - or * will perform subtraction or multiplication, not addition.
4fill in blank
hard

Fill both blanks to create an array with keys as strings and values as integers using type juggling.

PHP
<?php
$items = ["1" => 100, "2" => 200];
foreach ($items as $key => $value) {
    echo (int) $key [1] $value . "\n";
}
?>
Drag options to blanks, or click blank then click option'
A+
B.
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using . will join numbers as strings, not add them.
Using - or * will perform subtraction or multiplication, not addition.
5fill in blank
hard

Fill all three blanks to filter an array of strings, keeping only those that convert to integers greater than 10.

PHP
<?php
$values = ["5", "15", "abc", "20"];
$result = array_filter($values, function($val) {
    return (int) $val [1] 10;
});
print_r($result);
?>
Drag options to blanks, or click blank then click option'
A==
B>
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == will keep only values equal to 10, not greater.
Using < or != will filter incorrectly.

Practice

(1/5)
1. What does type juggling mean in PHP?
easy
A. PHP does not allow mixing strings and numbers
B. PHP requires manual type conversion for all operations
C. PHP throws errors when types do not match exactly
D. PHP automatically converts data types when needed in expressions

Solution

  1. Step 1: Understand PHP's behavior with mixed types

    PHP automatically changes data types behind the scenes when mixing strings and numbers.
  2. Step 2: Define type juggling

    This automatic conversion is called type juggling.
  3. Final Answer:

    PHP automatically converts data types when needed in expressions -> Option D
  4. Quick Check:

    Type juggling = automatic type conversion [OK]
Hint: Type juggling means PHP changes types automatically [OK]
Common Mistakes:
  • Thinking PHP never converts types automatically
  • Confusing type juggling with strict typing
  • Believing PHP throws errors on type mismatch
2. Which of the following is the correct way to compare two variables $a and $b without type juggling in PHP?
easy
A. if ($a == $b)
B. if ($a = $b)
C. if ($a === $b)
D. if ($a != $b)

Solution

  1. Step 1: Identify comparison operators

    == compares values with type juggling, === compares value and type strictly.
  2. Step 2: Choose strict comparison to avoid type juggling

    Using === ensures no automatic type conversion happens.
  3. Final Answer:

    if ($a === $b) -> Option C
  4. Quick Check:

    Strict comparison uses === [OK]
Hint: Use === to avoid type juggling in comparisons [OK]
Common Mistakes:
  • Using == which allows type juggling
  • Using = which is assignment, not comparison
  • Confusing != with strict inequality
3. What is the output of the following PHP code?
$a = '10 apples';
$b = 10;
var_dump($a == $b);
medium
A. bool(true)
B. string('10 apples')
C. int(10)
D. bool(false)

Solution

  1. Step 1: Understand comparison with == operator

    Using == triggers type juggling, so PHP converts the string to a number.
  2. Step 2: Convert string to number and compare

    '10 apples' converts to 10, so 10 == 10 is true.
  3. Final Answer:

    bool(true) -> Option A
  4. Quick Check:

    '10 apples' == 10 is true due to type juggling [OK]
Hint: == converts strings to numbers if possible [OK]
Common Mistakes:
  • Assuming string stays string in comparison
  • Expecting false because of extra text
  • Confusing var_dump output with print
4. Identify the error in this PHP code related to type juggling:
$x = '5 cats';
if ($x === 5) {
echo 'Match';
} else {
echo 'No match';
}
medium
A. No error, outputs 'No match'
B. No error, outputs 'Match'
C. Syntax error due to === operator
D. Runtime error due to string and int comparison

Solution

  1. Step 1: Understand strict comparison ===

    === checks both value and type without conversion.
  2. Step 2: Compare string '5 cats' with integer 5

    Types differ and values differ, so condition is false, prints 'No match'.
  3. Final Answer:

    No error, outputs 'No match' -> Option A
  4. Quick Check:

    Strict comparison fails, prints 'No match' [OK]
Hint: === compares type and value strictly, no conversion [OK]
Common Mistakes:
  • Expecting 'Match' because of numeric part
  • Thinking === causes syntax or runtime error
  • Confusing == and === behavior
5. Given the array $arr = ['1', 2, '3 cats', 4.0, '05'];, which code snippet correctly sums all numeric values considering PHP's type juggling?
hard
A.
$sum = 0;
foreach ($arr as $v) {
if (is_int($v)) $sum += $v;
}
echo $sum;
B.
$sum = 0;
foreach ($arr as $v) {
if (is_numeric($v)) $sum += $v;
}
echo $sum;
C.
$sum = 0;
foreach ($arr as $v) {
$sum += $v;
}
echo $sum;
D.
$sum = 0;
foreach ($arr as $v) {
if (gettype($v) === 'integer') $sum += $v;
}
echo $sum;

Solution

  1. Step 1: Understand the array contents and types

    Array has strings and numbers: '1'(string), 2(int), '3 cats'(string), 4.0(float), '05'(string).
  2. Step 2: Choose method to sum numeric values including numeric strings

    is_numeric() returns true for numeric strings and numbers, so it sums '1', 2, 4.0, '05' correctly. '3 cats' is ignored.
  3. Final Answer:

    Code snippet using is_numeric() -> Option B
  4. Quick Check:

    Use is_numeric() to sum numeric values including numeric strings [OK]
Hint: Use is_numeric() to include numeric strings in sums [OK]
Common Mistakes:
  • Using is_int() excludes numeric strings and floats
  • Using gettype() === 'integer' excludes floats and numeric strings
  • Adding all values without checking causes wrong sums
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