Bird
Raised Fist0
PHPprogramming~20 mins

Why type awareness matters in PHP - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Type Awareness Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code with type juggling?

Consider this PHP code snippet. What will it output?

PHP
<?php
$a = '5';
$b = 3;
echo $a + $b;
?>
AError: Unsupported operand types
B8
CTypeError
D'53'
Attempts:
2 left
💡 Hint

PHP automatically converts strings to numbers when using arithmetic operators.

❓ Predict Output
intermediate
2:00remaining
What error occurs when adding incompatible types in PHP 8+?

What happens when you run this PHP code?

PHP
<?php
$a = 'hello';
$b = 10;
echo $a + $b;
?>
A0
B10
CTypeError
Dhello10
Attempts:
2 left
💡 Hint

PHP 8+ is stricter about adding strings that cannot convert to numbers.

🔧 Debug
advanced
2:00remaining
Why does this PHP function return unexpected results?

Look at this PHP function and its output. Why does it return 'Result: 15' instead of 'Result: 510'?

PHP
<?php
function add($x, $y) {
    return $x + $y;
}

echo 'Result: ' . add('5', '10');
?>
ABecause '+' concatenates strings, so it should output '510'.
BBecause the function is missing a return statement.
CBecause PHP throws a TypeError when adding strings.
DBecause '+' adds numbers, so strings are converted to integers before addition.
Attempts:
2 left
💡 Hint

Think about how PHP treats '+' with string operands.

📝 Syntax
advanced
2:00remaining
Which PHP code snippet correctly enforces strict typing?

Which option correctly enables strict typing and declares a function that only accepts integers?

A
&lt;?php
declare(strict_types=0);
function multiply(int $a, int $b): int {
    return $a * $b;
}
?&gt;
B
&lt;?php
function multiply(int $a, int $b): int {
    return $a * $b;
}
declare(strict_types=1);
?&gt;
C
&lt;?php
declare(strict_types=1);
function multiply(int $a, int $b): int {
    return $a * $b;
}
?&gt;
D
&lt;?php
strict_types=1;
function multiply(int $a, int $b): int {
    return $a * $b;
}
?&gt;
Attempts:
2 left
💡 Hint

Strict typing must be declared at the top of the file before any code.

🚀 Application
expert
3:00remaining
What is the output of this PHP code using union types and strict typing?

Given this PHP code, what will it output?

PHP
<?php
declare(strict_types=1);
function formatValue(int|string $value): string {
    return match(gettype($value)) {
        'integer' => "Number: $value",
        'string' => "Text: $value",
        default => 'Unknown',
    };
}

echo formatValue(42) . ', ' . formatValue('hello');
?>
ANumber: 42, Text: hello
B42, hello
CText: 42, Number: hello
DTypeError
Attempts:
2 left
💡 Hint

Look at how match uses gettype to decide the output string.

Practice

(1/5)
1. Why is type awareness important in PHP programming?
easy
A. It makes the code run faster by skipping checks.
B. It helps prevent errors by knowing what kind of data is used.
C. It allows PHP to ignore variable names.
D. It removes the need for comments in code.

Solution

  1. Step 1: Understand what type awareness means

    Type awareness means knowing the kind of data (like number, text) your program uses.
  2. Step 2: Connect type awareness to error prevention

    Knowing data types helps avoid mistakes like adding text to numbers, which causes errors.
  3. Final Answer:

    It helps prevent errors by knowing what kind of data is used. -> Option B
  4. Quick Check:

    Type awareness = error prevention [OK]
Hint: Remember: types help catch mistakes early [OK]
Common Mistakes:
  • Thinking type awareness speeds up code by skipping checks
  • Believing variable names can be ignored
  • Assuming comments replace type knowledge
2. Which of the following is the correct way to declare a function with type hints in PHP?
easy
A. function add(int $a, int $b): int { return $a + $b; }
B. function add($a, $b) { return $a + $b; }
C. function add(int $a, $b): string { return $a + $b; }
D. function add(int $a, int $b) { return "$a + $b"; }

Solution

  1. Step 1: Identify correct type hint syntax

    PHP uses function name(type $param): returnType to declare types.
  2. Step 2: Check each option for correct types

    function add(int $a, int $b): int { return $a + $b; } correctly types both parameters as int and return type as int.
  3. Final Answer:

    function add(int $a, int $b): int { return $a + $b; } -> Option A
  4. Quick Check:

    Correct type hints = function add(int $a, int $b): int { return $a + $b; } [OK]
Hint: Look for parameter and return type hints together [OK]
Common Mistakes:
  • Missing return type declaration
  • Mixing types in parameters and return
  • Returning string when return type is int
3. What will be the output of this PHP code?
function multiply(int $x, int $y): int {
    return $x * $y;
}
echo multiply(3, '4 apples');
medium
A. 12
B. TypeError
C. 3 4 apples
D. 7

Solution

  1. Step 1: Understand PHP type coercion with int hints

    PHP converts string '4 apples' to integer 4 when type hinted as int.
  2. Step 2: Calculate the multiplication

    3 * 4 = 12, so the function returns 12 and echoes it.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    String '4 apples' coerced to int 4 = 12 output [OK]
Hint: PHP converts strings starting with numbers to int [OK]
Common Mistakes:
  • Expecting a TypeError for string argument
  • Thinking output is concatenated string
  • Adding numbers instead of multiplying
4. Identify the error in this PHP function and how to fix it:
function divide(int $a, int $b): float {
    return $a / $b;
}
echo divide(10, 0);
medium
A. No error; code runs fine.
B. Return type should be int, not float.
C. Parameters should be float, not int.
D. Division by zero error; check if $b is zero before dividing.

Solution

  1. Step 1: Analyze the division operation

    Dividing by zero causes a runtime error in PHP.
  2. Step 2: Suggest fix to avoid division by zero

    Check if $b is zero before dividing to prevent error.
  3. Final Answer:

    Division by zero error; check if $b is zero before dividing. -> Option D
  4. Quick Check:

    Division by zero check needed = Division by zero error; check if $b is zero before dividing. [OK]
Hint: Always check divisor is not zero before dividing [OK]
Common Mistakes:
  • Ignoring division by zero runtime error
  • Changing return type unnecessarily
  • Changing parameter types without reason
5. You want to create a PHP function that accepts an array of mixed values and returns an array with only integers doubled. Which approach best uses type awareness?
hard
A. Use no type hints and double all values assuming they are integers.
B. Declare function parameters as int to accept only integers and double them.
C. Use a function with array type hint and inside loop check if each item is int before doubling.
D. Use string type hint and convert all values to strings before doubling.

Solution

  1. Step 1: Understand the input and output requirements

    Input is an array with mixed types; output is array with only integers doubled.
  2. Step 2: Choose approach that uses type awareness correctly

    Using array type hint and checking each item's type inside loop ensures only integers are processed.
  3. Final Answer:

    Use a function with array type hint and inside loop check if each item is int before doubling. -> Option C
  4. Quick Check:

    Array type hint + type check inside loop = Use a function with array type hint and inside loop check if each item is int before doubling. [OK]
Hint: Type hint arrays, then check item types inside loop [OK]
Common Mistakes:
  • Using int type hint for whole array parameter
  • Skipping type checks and processing all items
  • Converting all values to strings unnecessarily
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