PHP – Function strict type Arguments

0 0
Read Time:1 Minute, 14 Second

Function strict type Arguments

PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.

In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict declaration, it will throw a “Fatal Error” if the data type mismatches.

Example:

In the following example we try to send both a number and a string to the function without using strict:

<?php
function addNumbers(int $a, int $b) {
  return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is NOT enabled "5 days" is changed to int(5),
// and it will return 10
?>

To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file.

Example:

In the following example we try to send both a number and a string to the function, but here we have added the strict declaration:

<?php 
declare(strict_types=1); // strict requirement

function addNumbers(int $a, int $b) {
  return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is enabled and "5 days" is not an integer, 
//an error will be thrown
?>

The strict declaration forces things to be used in the intended way.

W3Schools
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment