Challenge - 5 Problems
PHP Type Declarations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with typed parameters
What is the output of this PHP code when calling
greet('Alice')?PHP
<?php function greet(string $name) { return "Hello, $name!"; } echo greet('Alice');
Attempts:
2 left
💡 Hint
The function expects a string parameter and returns a greeting.
✗ Incorrect
The function greet requires a string parameter. Passing 'Alice' is valid, so it returns 'Hello, Alice!'.
❓ Predict Output
intermediate2:00remaining
Type error with wrong parameter type
What happens when this PHP code runs?
PHP
<?php function square(int $num) { return $num * $num; } echo square('5');
Attempts:
2 left
💡 Hint
PHP can convert strings to integers automatically in some cases.
✗ Incorrect
PHP converts the string '5' to integer 5 automatically, so the function returns 25.
❓ Predict Output
advanced2:00remaining
Return type declaration with parameter types
What is the output of this PHP code?
PHP
<?php function add(float $a, float $b): float { return $a + $b; } echo add(2, 3.5);
Attempts:
2 left
💡 Hint
PHP converts integers to floats when needed for float parameters.
✗ Incorrect
The integer 2 is converted to float 2.0, so the sum is 5.5 as a float.
❓ Predict Output
advanced2:00remaining
Nullable type declaration for parameter
What is the output of this PHP code?
PHP
<?php function welcome(?string $name) { if ($name === null) { return 'Welcome, guest!'; } return "Welcome, $name!"; } echo welcome(null);
Attempts:
2 left
💡 Hint
The question mark before string means the parameter can be null.
✗ Incorrect
The parameter accepts null, so the function returns 'Welcome, guest!' when null is passed.
❓ Predict Output
expert3:00remaining
Class type declaration for parameter
What is the output of this PHP code?
PHP
<?php class User { public string $name; public function __construct(string $name) { $this->name = $name; } } function greetUser(User $user) { return "Hello, {$user->name}!"; } $user = new User('Bob'); echo greetUser($user);
Attempts:
2 left
💡 Hint
The function expects an object of class User.
✗ Incorrect
The object $user is an instance of User with name 'Bob', so the function returns 'Hello, Bob!'.