Challenge - 5 Problems
Modern PHP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of typed properties in PHP 7.4+
What is the output of this PHP code using typed properties introduced in PHP 7.4?
PHP
<?php class User { public int $id; public string $name; public function __construct(int $id, string $name) { $this->id = $id; $this->name = $name; } } $user = new User(1, "Alice"); echo $user->id . ' - ' . $user->name;
Attempts:
2 left
💡 Hint
Typed properties require initialization before use.
✗ Incorrect
Typed properties in PHP 7.4 allow declaring variable types in classes. Here, both properties are initialized in the constructor, so accessing them prints their values.
🧠 Conceptual
intermediate1:30remaining
Why use PHP 8's union types?
Which of the following best explains the benefit of union types introduced in PHP 8?
Attempts:
2 left
💡 Hint
Think about how specifying multiple types helps programmers.
✗ Incorrect
Union types let you declare that a variable or parameter can be one of several types, making code more flexible and safer by catching type errors early.
🔧 Debug
advanced2:00remaining
Identify the error with nullsafe operator
What error does this PHP 8 code produce?
PHP
<?php class Profile { public ?string $email = null; } $user = null; echo $user?->profile?->email;
Attempts:
2 left
💡 Hint
The nullsafe operator returns null if the variable is null.
✗ Incorrect
The nullsafe operator (?->) safely accesses properties or methods on objects that might be null. Here, $user is null, so the expression returns null and prints nothing.
📝 Syntax
advanced2:00remaining
Correct syntax for match expression
Which option shows the correct syntax for PHP 8's match expression?
PHP
switch ($status) { case 1: $result = 'One'; break; case 2: $result = 'Two'; break; default: $result = 'Other'; }
Attempts:
2 left
💡 Hint
Look for the arrow syntax and commas between cases.
✗ Incorrect
The match expression uses => arrows and commas to separate cases. Parentheses around the variable are required. Semicolons inside the match block cause syntax errors.
🚀 Application
expert2:30remaining
How does PHP 8's JIT improve performance?
Which statement best describes how PHP 8's Just-In-Time (JIT) compiler improves PHP performance?
Attempts:
2 left
💡 Hint
Think about how turning code into machine instructions helps speed.
✗ Incorrect
JIT compiles PHP bytecode into native machine code during execution, which can greatly speed up heavy computations compared to interpreting code line-by-line.