0
0
PHPprogramming~10 mins

Declare strict_types directive in PHP - Interactive Code Practice

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

Complete the code to enable strict types in PHP.

PHP
<?php
[1]

function add(int $a, int $b): int {
    return $a + $b;
}
Drag options to blanks, or click blank then click option'
Astrict_types=1;
Bdeclare(strict_types=0);
Cenable_strict_types();
Ddeclare(strict_types=1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'strict_types=1;' without 'declare()' function.
Setting strict_types to 0 instead of 1.
Calling a function like 'enable_strict_types()' which does not exist.
2fill in blank
medium

Complete the code to correctly declare strict types at the top of the PHP file.

PHP
<?php
[1]

// Your code here
Drag options to blanks, or click blank then click option'
Adeclare(strict_types=1);
Bdeclare(strict_types=0);
Cdeclare(strict=1);
Ddeclare(strict_types);
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the parentheses.
Using 'strict=1' instead of 'strict_types=1'.
Leaving out the value assignment.
3fill in blank
hard

Fix the error in the strict types declaration.

PHP
<?php
[1]

function multiply(int $x, int $y): int {
    return $x * $y;
}
Drag options to blanks, or click blank then click option'
Adeclare(strict_types=);
Bdeclare(strict_types=1);
Cdeclare(strict_types=2);
Ddeclare(strict_types=0);
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the value empty.
Using values other than 0 or 1.
Using 0 which disables strict typing.
4fill in blank
hard

Fill both blanks to correctly declare strict types and define a function with strict typing.

PHP
<?php
[1]

function divide(int $a, int $b): float {
    return $a [2] $b;
}
Drag options to blanks, or click blank then click option'
Adeclare(strict_types=1);
B+
C/
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '*' instead of '/' for division.
Omitting the strict types declaration.
Using incorrect syntax for declare.
5fill in blank
hard

Fill all three blanks to declare strict types, define a function, and return the sum of two integers.

PHP
<?php
[1]

function sum([2] $x, [3] $y): int {
    return $x + $y;
}
Drag options to blanks, or click blank then click option'
Adeclare(strict_types=1);
Bint
Cfloat
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using float or string types for parameters when return type is int.
Omitting strict types declaration.
Incorrect syntax in declare statement.