0
0
PHPprogramming~20 mins

Why strict typing matters in PHP - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Strict Typing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when strict typing is enabled?

Consider this PHP code with strict typing enabled. What will it output?

PHP
<?php
declare(strict_types=1);
function addInts(int $a, int $b): int {
    return $a + $b;
}
echo addInts(5, '10');
A15
B5
C510
DTypeError
Attempts:
2 left
💡 Hint

Strict typing enforces exact types for function parameters.

Predict Output
intermediate
2:00remaining
What happens without strict typing?

What will this PHP code output when strict typing is not enabled?

PHP
<?php
function addInts(int $a, int $b): int {
    return $a + $b;
}
echo addInts(5, '10');
ATypeError
B510
C15
D5
Attempts:
2 left
💡 Hint

Without strict typing, PHP tries to convert types automatically.

🔧 Debug
advanced
2:00remaining
Identify the error with strict typing

What error will this code produce when strict typing is enabled?

PHP
<?php
declare(strict_types=1);
function greet(string $name): string {
    return 'Hello, ' . $name;
}
echo greet(123);
ATypeError
BHello, 123
CParse error
DFatal error: Uncaught Error
Attempts:
2 left
💡 Hint

Check the type of argument passed to the function.

🧠 Conceptual
advanced
2:00remaining
Why use strict typing in PHP?

Which of the following is the best reason to enable strict typing in PHP?

AIt makes PHP run faster by skipping type checks
BIt helps catch type-related bugs early by enforcing exact types
CIt allows mixing types freely without errors
DIt automatically converts all types to strings
Attempts:
2 left
💡 Hint

Think about how strict typing affects error detection.

Predict Output
expert
2:00remaining
What is the output with mixed types and strict typing?

Given strict typing is enabled, what will this code output?

PHP
<?php
declare(strict_types=1);
function multiply(float $a, float $b): float {
    return $a * $b;
}
$result = multiply(3, 2.5);
echo gettype($result) . ': ' . $result;
Afloat: 7.5
Bint: 7
CTypeError
Dstring: 7.5
Attempts:
2 left
💡 Hint

Check how PHP handles integers passed to float parameters with strict typing.