Strict typing helps catch mistakes early by making sure data is the right type. It stops bugs before they happen.
Why strict typing matters in PHP
<?php declare(strict_types=1); function add(int $a, int $b): int { return $a + $b; } echo add(5, 10);
declare(strict_types=1); turns on strict typing for the file.
Function parameters and return types are declared with types like int.
<?php // Without strict typing function multiply($a, $b) { return $a * $b; } echo multiply('3', 4); // Works but '3' is a string
<?php declare(strict_types=1); function multiply(int $a, int $b): int { return $a * $b; } // echo multiply('3', 4); // Error: string given, int expected
This program shows how strict typing requires the argument to be a string. Passing a number would cause an error.
<?php // Enable strict typing declare(strict_types=1); function greet(string $name): string { return "Hello, $name!"; } // Correct usage echo greet("Alice") . "\n"; // Uncommenting the next line will cause an error because 123 is not a string // echo greet(123);
Strict typing only applies when you add declare(strict_types=1); at the top of the PHP file.
Without strict typing, PHP tries to convert types automatically, which can hide bugs.
Strict typing helps make your code safer and easier to understand.
Strict typing forces data to be the correct type, reducing bugs.
Use declare(strict_types=1); to enable it in PHP files.
It makes your code clearer and helps catch errors early.
Practice
declare(strict_types=1); in a PHP file?Solution
Step 1: Understand the role of strict typing in PHP
Strict typing ensures that the types of function arguments and return values are exactly as declared, preventing automatic type coercion.Step 2: Analyze the effect of
This declaration activates strict typing mode in the file, enforcing exact type matches and reducing bugs caused by unexpected type conversions.declare(strict_types=1);Final Answer:
To enforce that function arguments and return values must match declared types exactly -> Option DQuick Check:
Strict typing = enforce exact types [OK]
- Thinking strict typing speeds up code
- Believing it disables errors
- Assuming it allows type conversions
Solution
Step 1: Recall the syntax for enabling strict typing
PHP uses thedeclarestatement withstrict_types=1to enable strict typing at the start of the file.Step 2: Identify the correct syntax among options
Only uses the correctdeclare(strict_types=1);syntax inside PHP tags.Final Answer:
<?php declare(strict_types=1); ?> -> Option CQuick Check:
Enable strict typing = declare(strict_types=1) [OK]
- Using incorrect function calls to enable strict typing
- Missing the declare keyword
- Placing strict_types outside PHP tags
declare(strict_types=1);
function addInts(int $a, int $b): int {
return $a + $b;
}
echo addInts(5, '10');Solution
Step 1: Understand strict typing effect on function arguments
With strict typing enabled, PHP requires the argument types to match exactly. Passing a string '10' instead of int causes an error.Step 2: Predict the behavior of the function call
Since '10' is a string, PHP throws a TypeError instead of converting it to int automatically.Final Answer:
TypeError -> Option AQuick Check:
Strict typing + wrong type = TypeError [OK]
- Assuming PHP converts string '10' to int automatically
- Expecting output '510' by concatenation
- Ignoring strict typing effect
declare(strict_types=1);
function multiply(float $x, float $y): float {
return $x * $y;
}
echo multiply(3, '4.5');Solution
Step 1: Check argument types with strict typing
Strict typing requires exact types. Passing '4.5' as string instead of float causes a type mismatch.Step 2: Determine the error type
PHP throws a TypeError because it does not convert string to float automatically under strict typing.Final Answer:
TypeError because '4.5' is a string, not float -> Option BQuick Check:
Strict typing + string instead of float = TypeError [OK]
- Thinking PHP converts string '4.5' to float automatically
- Confusing TypeError with syntax error
- Expecting a warning instead of error
Solution
Step 1: Check strict typing declaration
usesdeclare(strict_types=1);to enable strict typing, which is required to enforce exact types.Step 2: Verify function argument types
declares the function to accept only integers usingint ...$nums, enforcing all arguments to be integers.Step 3: Compare other options
lacks strict typing and type hints, allowing any type. disables strict typing. lacks type hint for arguments.Final Answer:
correctly enforces strict typing and integer-only arguments -> Option AQuick Check:
Strict typing + int type hint = safe integer sum [OK]
- Omitting strict_types declaration
- Not using type hints for function parameters
- Using declare(strict_types=0) which disables strict typing
Ready to prove it?
Pass the test and this pattern moves unknown
