PHP type declarations or strict_types is a directive to use the strict mode for PHP type hinting which allows us to add a strict typing for function arguments values.
Table Of Contents
And that can be done using the following PHP directive.
declare( strict_types=1 );
In the following section, you will learn how to use the PHP strict_types with integer and float types, but before that you have to know how it works.

According to the PHP type hinting you can pass the variable types in the function parameters. That allows you passing the values with the same types when you call this function in another place.
For example.
<?php
function multiply ( int $x, int $y ) {
return $x * $y;
}
multiply( 2, 10 ); // 20
?>
In this example, you have to pass an integer type to the multiply($x, $y) function when you call it in another place.
But, PHP accepts also float types instead of integer types when you call this function. For example.
<?php
// I used the same function of the previous example
multiply( 2.5, 105.9 ); // 210
?>
This will work also without any problem because there is no strict mode here. So, when you use the strict mode here, it will show you an error. Let’s move to the following section to understand that.
Using the PHP Strict Typing “Type Declarations” for Function Arguments Values
As I mentioned, the strict_types is a strict mode, which can be passed into the declare directive and appears in the first statement of the script file. The basic expression can be like the below.
declare( strict_types=1 );
The strict_types in PHP, searching for the type and the value. It accepts 1 when you play the script as a strict mode to looking for the exact type and value, and 0 if only for the value.
Let’s use it with the same previous function example.
<?php
declare( strict_types = 1 );
function multiply ( int $x, int $y ) {
return $x * $y;
}
multiply( 2.5, 30.6 );
?>
This will show you an error like the below.
Fatal error: Uncaught TypeError: Argument 1 passed to multiply() must be of the type int, float given, called in D:\xampp\htdocs\site-1\index.php on line 9 and defined in D:\xampp\htdocs\site-1\index.php:5 Stack trace: #0 D:\xampp\htdocs\site-1\index.php(9): multiply(21.5, 10.15) #1 {main} thrown in D:\xampp\htdocs\site-1\index.php on line 5
This means you should pass the same type value of the function arguments, so if it is an integer type, it only accepts an integer value.
<?php
multiply( 25, 306 ); // 7650
?>
But, What about if the main argument type is a float, and you passed an integer value ? Let’s see what will happen in the following section.
The following shows you how to pass an integer value into a float type argument using the PHP strict_types.
PHP allows you to pass integer data type into float data type using the strict_types mode. For example.
<?php
declare( strict_types = 1 );
function sum ( float $x, float $y ) {
return $x + $y;
}
sum( 25, 55 ); // 80 => float type
?>
The output of this function would be a float value because it was two numbers as argument with float data types. But when you print it, it will show you an integer value.
This hypothesis is wrong because when you print the value, the output would print 80.0.
But PHP not looking for the precision if it has zero value, so the final output would be 80 and not 80.0 when the type is a float number.
Another thing, if you passed a float into an integer type, it will show you an error which is a Fatal error type as you saw in the previous section.
Using the PHP Strict Typing “strict_types“ for Function Type
Actually, when you pass 2 integer numbers as values into function arguments that already accepts float data types. It will return float data type automatically.
But what about if the PHP function type is returning an integer data type. Let’s see am example.
<?php
declare( strict_types = 1 );
function sum ( float $x, float $y ) : int {
return $x + $y;
}
sum( 100, 100 );
?>
This will show you an error like the below.
Fatal error: Uncaught TypeError: Return value of sum() must be of the type int, float returned in D:\xampp\htdocs\site-1\index.php:6 Stack trace: #0 D:\xampp\htdocs\site-1\index.php(9): sum(100, 100) #1 {main} thrown in D:\xampp\htdocs\site-1\index.php on line 6
That will happen because the returned data type is a float and it should return an integer. The output that appears for the interpreter is 100.0 + 100.0 = 200.0 and not 100 + 100 = 200. But PHP doesn’t show you that.
If you need to ensure from that you have to use var_dump function. So the correct would be a minor change in the function parameter types to be an integer or in the function type to be a float.
<?php
declare( strict_types = 1 );
function sum ( float $x, float $y ) : float{
return $x + $y;
}
sum( 100, 100 ); // 200
?>
Or
<?php
declare( strict_types = 1 );
function sum ( int $x, int $y ) : int{
return $x + $y;
}
sum( 100, 100 ); // 200
?>
Notes for the PHP strict_types or the Type Declarations
If a PHP file including another file and the other one contains the PHP declaration directive. This will not affect in the caller because of the PHP type declarations only work per PHP script file. For example.
The below shows you the coding of PHP inc.php file.
<?php
// file name inc.php
declare( strict_types = 1 );
function calc ( int $x, int $y ): int {
return $x - $y;
}
?>
And in the main.php file I will invoke the calc( $x, $y ) function with float data types.
<?php
// file name main.php
include_once "inc.php";
calc ( 10.9, 5.2 );
?>
This will print: 5 and it will not show errors because PHP type declarations was only for the first file and you can only do one directive for all files. And that can be done for the main file and not for the included files. So the correct would be like the below.
<?php
// file name inc.php
function calc ( int $x, int $y ): int {
return $x - $y;
}
?>
And the main file.
<?php
// file name main.php
declare( strict_types = 1 );
include_once "inc.php";
calc ( 10.9, 5.2 );
?>
This will show you the correct result for the strict mode.
Anyway, the declare directive should be in the first statement of PHP otherwise, it will show you the below fatal error.
Fatal error: strict_types declaration must be the very first statement in the script in C:\xampp\htdocs\wordpress\inc.php on line 6
Wrapping Up
- PHP type declarations or strict_types is a directive to use the strict mode for PHP type hinting.
- It can work with the type hinting of PHP function parameters and also, with the function type.
- The location of the strict_types declaration should be in the first statement in the PHP script.