The declare(strict_types=1); directive makes PHP check the exact types of values passed to functions. This helps catch mistakes early and keeps your code safer.
Declare strict_types directive in PHP
<?php declare(strict_types=1); // Your PHP code here
This directive must be the very first statement in the PHP file, before any other code or whitespace.
It only affects type checking for function calls in the same file.
add only accepts integers exactly.<?php declare(strict_types=1); function add(int $a, int $b): int { return $a + $b; } echo add(5, 10);
declare(strict_types=1);, PHP converts "10" to 10 automatically.<?php function add(int $a, int $b): int { return $a + $b; } // Without strict_types, this works: echo add(5, "10");
This program multiplies two numbers. Because of strict types, both must be floats exactly. The commented line shows what happens if types are wrong.
<?php declare(strict_types=1); function multiply(float $x, float $y): float { return $x * $y; } // Correct types echo multiply(2.5, 4.0) . "\n"; // Incorrect type - this will cause a TypeError // echo multiply(2, "3");
If you forget to put declare(strict_types=1); at the top, PHP will not check types strictly.
Strict types only affect scalar types like int, float, string, and bool.
Type errors caused by strict types help you find bugs early.
declare(strict_types=1); makes PHP check exact types for function calls.
Put it at the very top of your PHP file before any code.
It helps catch bugs and makes your code safer and clearer.
Practice
declare(strict_types=1); directive do in a PHP file?Solution
Step 1: Understand the purpose of strict_types
The directivedeclare(strict_types=1);tells PHP to enforce exact type matching for function arguments and return values.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.Final Answer:
It forces PHP to check that function arguments match their declared types exactly. -> Option DQuick Check:
strict_types enforces exact type checks = A [OK]
- Thinking it disables errors
- Confusing with timezone settings
- Assuming it enables type coercion
Solution
Step 1: Check correct syntax for declare directive
The correct syntax isdeclare(strict_types=1);placed at the top of the PHP file.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); ?> usestrueinstead of1, which is invalid for this directive.Final Answer:
<?php declare(strict_types=1); ?> -> Option CQuick Check:
declare(strict_types=1) is correct syntax = C [OK]
- Using true instead of 1
- Trying to assign strict_types as a variable
- Calling a non-existent function
<?php
declare(strict_types=1);
function add(int $a, int $b): int {
return $a + $b;
}
echo add(5, '3');
?>What will be the output?
Solution
Step 1: Understand strict_types effect on function calls
Withdeclare(strict_types=1);, PHP requires exact types. Passing a string '3' to a parameter typed as int causes an error.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.Final Answer:
TypeError: Argument 2 passed must be of the type int, string given -> Option AQuick Check:
strict_types=1 + wrong argument type = TypeError [OK]
- Assuming PHP converts string '3' to int automatically
- Expecting output 8 or 53
- Confusing warning with error
<?php
function multiply(int $x, int $y): int {
return $x * $y;
}
declare(strict_types=1);
echo multiply(4, 5);
?>Solution
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.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.Final Answer:
The declare(strict_types=1); must be at the top before any code. -> Option AQuick Check:
declare(strict_types=1) must be first line = B [OK]
- Placing declare after code or functions
- Thinking return type must be string
- Removing type declarations from parameters
Solution
Step 1: Enable strict types at the file start
To prevent type coercion, adddeclare(strict_types=1);at the very top of the PHP file.Step 2: Define the function with int type hints
Declare the function parameters and return type as int to enforce integer-only usage.Final Answer:
Place declare(strict_types=1); at the top, then define the function with int types. -> Option BQuick Check:
strict_types=1 + int types = strict integer enforcement [OK]
- Skipping declare(strict_types=1);
- Using declare(strict_types=0); which allows coercion
- Not adding type hints in function
Ready to prove it?
Pass the test and this pattern moves unknown
