0
0
PHPprogramming~10 mins

Why modern PHP matters - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print a simple message using modern PHP syntax.

PHP
<?php

echo [1];

?>
Drag options to blanks, or click blank then click option'
A"Hello, modern PHP!"
BHello, modern PHP!
C'Hello, modern PHP!'
Dprint("Hello, modern PHP!")
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string.
Using print() inside echo statement.
2fill in blank
medium

Complete the code to declare a typed property in a PHP class.

PHP
<?php

class User {
    [1] string $name;
}

?>
Drag options to blanks, or click blank then click option'
Avar
Bprotected
Cpublic
Dprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' which is deprecated in modern PHP.
Using private or protected when public is needed.
3fill in blank
hard

Fix the error in the function declaration using modern PHP return type syntax.

PHP
<?php

function getCount(): [1] {
    return 5;
}

?>
Drag options to blanks, or click blank then click option'
Abool
Bvoid
Cstring
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' or 'bool' as return type when returning an integer.
Using 'void' when the function returns a value.
4fill in blank
hard

Fill both blanks to create a typed array property with default empty array in a PHP class.

PHP
<?php

class Product {
    private array [1] = [2];
}

?>
Drag options to blanks, or click blank then click option'
A$tags
B[]
Carray()
D$items
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array()' instead of '[]' for empty array.
Forgetting the dollar sign in property name.
5fill in blank
hard

Fill all three blanks to create a typed function with nullable parameter and return type in modern PHP.

PHP
<?php

function findUser([1] ?int $id): ?[2] {
    if ($id === null) {
        return null;
    }
    return new [3]();
}

?>
Drag options to blanks, or click blank then click option'
Apublic
BUser
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Adding visibility keyword to function parameter.
Mismatch between return type and returned object.