0
0
PHPprogramming~10 mins

Why type awareness matters in PHP - Test Your Understanding

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

Complete the code to declare a variable with an integer type.

PHP
<?php
$number = [1];
?>
Drag options to blanks, or click blank then click option'
A42
B"42"
Ctrue
D3.14
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around numbers makes them strings, not integers.
2fill in blank
medium

Complete the code to declare a function that returns a string type.

PHP
<?php
function greet(): [1] {
    return "Hello!";
}
?>
Drag options to blanks, or click blank then click option'
Aint
Bfloat
Cbool
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong return type causes type errors.
3fill in blank
hard

Fix the error in the code by completing the type declaration for the parameter.

PHP
<?php
function doubleValue([1] $num): int {
    return $num * 2;
}
?>
Drag options to blanks, or click blank then click option'
Astring
Bbool
Cint
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using string or bool causes type mismatch errors.
4fill in blank
hard

Fill both blanks to create an array with integer keys and string values.

PHP
<?php
$fruits = array([1] => [2]);
?>
Drag options to blanks, or click blank then click option'
A1
B"apple"
C"banana"
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using boolean values as keys or values causes unexpected behavior.
5fill in blank
hard

Fill all three blanks to declare a typed class property and assign it a value.

PHP
<?php
class Person {
    public [1] $name = [2];
    public [3] $age = 30;
}
?>
Drag options to blanks, or click blank then click option'
Astring
B"John"
Cint
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a string value to an integer property or vice versa.