Bird
Raised Fist0
PHPprogramming~10 mins

Declare strict_types directive in PHP - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does the declare(strict_types=1); directive do in a PHP file?
easy
A. It enables automatic type conversion for function arguments.
B. It disables all error reporting in the PHP script.
C. It sets the default timezone for the script.
D. It forces PHP to check that function arguments match their declared types exactly.

Solution

  1. Step 1: Understand the purpose of strict_types

    The directive declare(strict_types=1); tells PHP to enforce exact type matching for function arguments and return values.
  2. Step 2: Compare with other options

    Options B, C, and D describe unrelated PHP features: error reporting, timezone setting, and type conversion, which are not controlled by this directive.
  3. Final Answer:

    It forces PHP to check that function arguments match their declared types exactly. -> Option D
  4. Quick Check:

    strict_types enforces exact type checks = A [OK]
Hint: Strict types means exact argument types only [OK]
Common Mistakes:
  • Thinking it disables errors
  • Confusing with timezone settings
  • Assuming it enables type coercion
2. Which of the following is the correct way to enable strict types in a PHP file?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Check correct syntax for declare directive

    The correct syntax is declare(strict_types=1); placed at the top of the PHP file.
  2. Step 2: Analyze each option

    <?php declare(strict_types=1); ?> uses the exact correct syntax. Options A and B use invalid PHP syntax. <?php declare(strict_types = true); ?> uses true instead of 1, which is invalid for this directive.
  3. Final Answer:

    <?php declare(strict_types=1); ?> -> Option C
  4. Quick Check:

    declare(strict_types=1) is correct syntax = C [OK]
Hint: Use declare(strict_types=1); at file top [OK]
Common Mistakes:
  • Using true instead of 1
  • Trying to assign strict_types as a variable
  • Calling a non-existent function
3. Given this PHP code with strict types enabled:
<?php
declare(strict_types=1);
function add(int $a, int $b): int {
    return $a + $b;
}
echo add(5, '3');
?>

What will be the output?
medium
A. TypeError: Argument 2 passed must be of the type int, string given
B. 8
C. 53
D. Warning: Implicit conversion from string to int

Solution

  1. Step 1: Understand strict_types effect on function calls

    With declare(strict_types=1);, PHP requires exact types. Passing a string '3' to a parameter typed as int causes an error.
  2. Step 2: Predict the runtime behavior

    PHP throws a TypeError because the second argument is a string, not an int, and no automatic conversion happens.
  3. Final Answer:

    TypeError: Argument 2 passed must be of the type int, string given -> Option A
  4. Quick Check:

    strict_types=1 + wrong argument type = TypeError [OK]
Hint: Strict types cause TypeError on wrong argument types [OK]
Common Mistakes:
  • Assuming PHP converts string '3' to int automatically
  • Expecting output 8 or 53
  • Confusing warning with error
4. Identify the error in this PHP code snippet:
<?php
function multiply(int $x, int $y): int {
    return $x * $y;
}

declare(strict_types=1);
echo multiply(4, 5);
?>
medium
A. The declare(strict_types=1); must be at the top before any code.
B. The function multiply must return a string, not int.
C. The function parameters should not have types declared.
D. There is no error; the code runs fine.

Solution

  1. Step 1: Check position of declare(strict_types=1);

    The strict_types directive must be declared at the very top of the PHP file, before any code or whitespace.
  2. Step 2: Analyze the code order

    Here, the function is declared before the strict_types directive, which is invalid and causes the directive to be ignored or cause an error.
  3. Final Answer:

    The declare(strict_types=1); must be at the top before any code. -> Option A
  4. Quick Check:

    declare(strict_types=1) must be first line = B [OK]
Hint: Put declare(strict_types=1); at file very top [OK]
Common Mistakes:
  • Placing declare after code or functions
  • Thinking return type must be string
  • Removing type declarations from parameters
5. You want to write a PHP function that accepts only integers and returns their sum. You also want to ensure no accidental type coercion happens anywhere in the file. How should you set up your PHP file?
hard
A. Use no type declarations and rely on PHP's automatic type conversion.
B. Place declare(strict_types=1); at the top, then define the function with int types.
C. Use declare(strict_types=0); and define the function with int types.
D. Define the function with int types but do not use declare(strict_types=1);

Solution

  1. Step 1: Enable strict types at the file start

    To prevent type coercion, add declare(strict_types=1); at the very top of the PHP file.
  2. Step 2: Define the function with int type hints

    Declare the function parameters and return type as int to enforce integer-only usage.
  3. Final Answer:

    Place declare(strict_types=1); at the top, then define the function with int types. -> Option B
  4. Quick Check:

    strict_types=1 + int types = strict integer enforcement [OK]
Hint: Use declare(strict_types=1); plus int type hints [OK]
Common Mistakes:
  • Skipping declare(strict_types=1);
  • Using declare(strict_types=0); which allows coercion
  • Not adding type hints in function
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