PHP is a Loosely Typed Language

PHP is 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.

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

function addNumbers(int $x, int $y) {
  return $x + $y;
}
echo addNumbers(7, "7 month");
// since strict is NOT enabled "7 month" is changed to int(7), and it will return 14
?>
Scroll to Top