Bird
Raised Fist0
PHPprogramming~20 mins

Why modern PHP matters - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Modern PHP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
A1 - Alice
BError: Typed property must be initialized
C1-Alice
D1, Alice
Attempts:
2 left
💡 Hint
Typed properties require initialization before use.
🧠 Conceptual
intermediate
1:30remaining
Why use PHP 8's union types?
Which of the following best explains the benefit of union types introduced in PHP 8?
AThey allow a variable to accept multiple specified types, improving code clarity and error checking.
BThey automatically convert all types to strings for easier output.
CThey remove the need for any type declarations in functions.
DThey force all variables to be of a single type only.
Attempts:
2 left
💡 Hint
Think about how specifying multiple types helps programmers.
🔧 Debug
advanced
2: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;
ANotice: Undefined variable 'user'
BFatal error: Trying to get property of non-object
CParse error: syntax error near '?->'
DNo output, prints nothing
Attempts:
2 left
💡 Hint
The nullsafe operator returns null if the variable is null.
📝 Syntax
advanced
2: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';
}
A
$result = match($status) {
    1 =&gt; 'One';
    2 =&gt; 'Two';
    default =&gt; 'Other';
};
B
$result = match($status) {
    1: 'One',
    2: 'Two',
    default: 'Other'
};
C
$result = match($status) {
    1 =&gt; 'One',
    2 =&gt; 'Two',
    default =&gt; 'Other',
};
D
$result = match $status {
    1 =&gt; 'One',
    2 =&gt; 'Two',
    default =&gt; 'Other'
};
Attempts:
2 left
💡 Hint
Look for the arrow syntax and commas between cases.
🚀 Application
expert
2: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?
AJIT caches PHP source files to reduce disk reads.
BJIT compiles PHP code into machine code at runtime, speeding up CPU-intensive tasks.
CJIT converts PHP code into JavaScript for faster browser execution.
DJIT removes all type checks to make code run faster.
Attempts:
2 left
💡 Hint
Think about how turning code into machine instructions helps speed.

Practice

(1/5)
1. Why is using modern PHP versions important for developers?
easy
A. Because it offers new features that improve code safety and readability
B. Because older PHP versions run faster on all servers
C. Because modern PHP removes all bugs automatically
D. Because it allows running PHP code without a web server

Solution

  1. Step 1: Understand the benefits of modern PHP

    Modern PHP versions introduce features like typed properties and arrow functions that help write safer and clearer code.
  2. Step 2: Evaluate the options

    Only Because it offers new features that improve code safety and readability correctly states the importance of new features improving safety and readability. Other options are incorrect or misleading.
  3. Final Answer:

    Because it offers new features that improve code safety and readability -> Option A
  4. Quick Check:

    Modern PHP = safer, clearer code [OK]
Hint: Focus on benefits of new features in modern PHP [OK]
Common Mistakes:
  • Thinking older PHP is always faster
  • Believing modern PHP fixes bugs automatically
  • Confusing PHP execution environment
2. Which of the following is the correct syntax for an arrow function introduced in modern PHP?
easy
A. function($x) { return $x * 2; }
B. fn($x) => $x * 2;
C. arrow($x) => $x * 2;
D. fn $x => { $x * 2 }

Solution

  1. Step 1: Recall arrow function syntax in PHP

    Modern PHP uses fn($params) => expression; for arrow functions.
  2. Step 2: Compare options

    fn($x) => $x * 2; matches the correct syntax. function($x) { return $x * 2; } is a traditional anonymous function. Options C and D have invalid syntax.
  3. Final Answer:

    fn($x) => $x * 2; -> Option B
  4. Quick Check:

    Arrow function syntax = fn(params) => expression [OK]
Hint: Look for 'fn' keyword with arrow => syntax [OK]
Common Mistakes:
  • Using 'function' keyword for arrow functions
  • Missing parentheses around parameters
  • Incorrect placement of braces or arrows
3. What will be the output of this PHP code using typed properties?
class User {
  public int $id;
  public string $name;
  public function __construct(int $id, string $name) {
    $this->id = $id;
    $this->name = $name;
  }
}
$user = new User(5, 'Anna');
echo $user->id . ' - ' . $user->name;
medium
A. Error: Typed property must be initialized
B. 0 -
C. 5 - Anna
D. Anna - 5

Solution

  1. Step 1: Understand typed properties initialization

    The constructor sets $id to 5 and $name to 'Anna', so properties are properly initialized.
  2. Step 2: Check output of echo statement

    Echo prints $user->id (5), then ' - ', then $user->name ('Anna').
  3. Final Answer:

    5 - Anna -> Option C
  4. Quick Check:

    Typed properties initialized correctly = 5 - Anna [OK]
Hint: Check constructor sets typed properties before use [OK]
Common Mistakes:
  • Assuming uninitialized typed properties cause error here
  • Mixing order of output values
  • Confusing property types with values
4. Identify the error in this modern PHP code snippet:
price = $price;
  }
}
$product = new Product('100');
echo $product->price;
medium
A. Fatal error: Cannot access property
B. No error, outputs 100
C. Syntax error due to missing semicolon
D. TypeError because string '100' is passed instead of int

Solution

  1. Step 1: Check constructor parameter type

    The constructor expects an int for $price.
  2. Step 2: Analyze argument passed

    The argument passed is a string '100', which causes a TypeError in strict typing mode.
  3. Final Answer:

    TypeError because string '100' is passed instead of int -> Option D
  4. Quick Check:

    Typed property expects int, string given = TypeError [OK]
Hint: Match argument types exactly with typed properties [OK]
Common Mistakes:
  • Assuming PHP auto-converts string to int in strict mode
  • Ignoring type declarations in constructor
  • Confusing syntax errors with type errors
5. You want to update legacy PHP code to use modern features for better security and performance. Which approach best applies modern PHP advantages?
hard
A. Add typed properties, use arrow functions, and enable strict types
B. Remove all type hints and use global variables for speed
C. Keep old code but add comments about modern PHP
D. Use eval() to run code faster and avoid errors

Solution

  1. Step 1: Identify modern PHP features for improvement

    Typed properties, arrow functions, and strict typing improve code safety and performance.
  2. Step 2: Evaluate options for best practice

    Add typed properties, use arrow functions, and enable strict types applies these features correctly. Options A, B, and D either reduce safety or do not improve code.
  3. Final Answer:

    Add typed properties, use arrow functions, and enable strict types -> Option A
  4. Quick Check:

    Modern PHP = typed properties + arrow functions + strict types [OK]
Hint: Use new PHP features to improve code safety and speed [OK]
Common Mistakes:
  • Ignoring type safety for speed
  • Relying on eval() which is insecure
  • Not updating code, only commenting
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