0
0
PHPprogramming~10 mins

Declare strict_types directive in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Declare strict_types directive
Start PHP file
Declare strict_types=1?
NoDefault weak typing
Yes
Enable strict type checking
Run code with strict types
Type errors if mismatch
End
The strict_types directive tells PHP to enforce exact types for function calls, stopping automatic type conversion.
Execution Sample
PHP
<?php
declare(strict_types=1);
function add(int $a, int $b): int {
  return $a + $b;
}
echo add(1, 2);
This code enables strict types and defines a function that adds two integers, then prints the result.
Execution Table
StepActionEvaluationResult
1Start script with declare(strict_types=1);strict_types enabledStrict mode ON
2Call add(1, 2)Check argument typesBoth are int, OK
3Execute add functionReturn 1 + 23
4Print resultOutput 33
5Call add('1', 2)Check argument typesTypeError thrown (string not int)
💡 Execution stops with TypeError if argument types do not exactly match when strict_types=1
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5
$aundefined11TypeError (no value)
$bundefined22TypeError (no value)
Return valueundefinedundefined3No return due to error
Key Moments - 2 Insights
Why does add('1', 2) cause an error with strict_types=1?
Because strict_types=1 requires exact types. The string '1' is not automatically converted to int, so PHP throws a TypeError as shown in execution_table step 5.
What happens if declare(strict_types=1) is missing?
PHP uses weak typing and automatically converts types. So add('1', 2) would work by converting '1' to int 1, no error occurs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 4?
A3
BTypeError
C1
DNo output
💡 Hint
Check the 'Result' column at step 4 in execution_table
At which step does the TypeError occur when calling add('1', 2)?
AStep 2
BStep 5
CStep 3
DStep 4
💡 Hint
Look for the row mentioning TypeError in execution_table
If declare(strict_types=1) was removed, what would happen at step 5?
AScript stops immediately
BTypeError still thrown
CFunction call succeeds with automatic type conversion
DOutput is empty
💡 Hint
Refer to key_moments explanation about behavior without strict_types
Concept Snapshot
declare(strict_types=1);
- Placed at top of PHP file
- Enables strict type checking for function calls
- Requires exact argument types (no automatic conversion)
- Causes TypeError if types mismatch
- Helps catch bugs early by enforcing types
Full Transcript
This visual trace shows how the declare(strict_types=1) directive in PHP enforces strict type checking. When enabled, PHP requires function arguments to match declared types exactly. The example code defines a function add(int $a, int $b) and calls it with integers 1 and 2, which works fine and outputs 3. However, calling add('1', 2) causes a TypeError because the string '1' is not automatically converted to int under strict types. The variable tracker shows how $a and $b get values or cause errors. Key moments clarify why strict types cause errors and what happens if the directive is missing. The quiz tests understanding of output, error steps, and behavior changes without strict types.