“weak typing” vs “strict types” in PHP

Erland Muchasaj
4 min readJun 6, 2023
“weak typing” vs “strict types”
“weak typing” vs “strict types”

In PHP, the concepts of loose typing and strict typing refer to how the language handles variable types and type conversions.

For example, you can initialize a variable with an integer value.
Then add a float value to it, thereby turning it into a float, then join it onto a string value to produce a longer string.

Since PHP 5 you can use type hinting to specify the expected data type of an argument in a function declaration. When you call the function, PHP will check whether or not the arguments are of the specified type. If not, the run-time will raise an error and execution will be halted.

Week typing aka Loose Typing

By default, PHP follows loose typing or weak typing, which allows for implicit type conversions and flexible type handling. In loose typing, variables do not have an explicitly declared type, and they can be reassigned with different types of values during execution.

For example:

$number = 10;   // $number is initially assigned an integer value
$number = "20"; // $number is now reassigned with a string value

In this example, the variable $number is initially assigned an integer value but is later reassigned with a string value. PHP automatically performs the necessary type conversion to accommodate the new value without generating an error.

Strict Typing

PHP also provides the option for strict typing, which enforces stronger type-checking and requires explicit type declarations. When strict typing is enabled, variables and function parameters must strictly adhere to their declared types. Any attempt to assign a value of a different type or incompatible type will result in a fatal error.

Let us see an example:

function addNumbers(int $a, int $b): int
{
return $a + $b;
}

addNumbers(10, 20); // 30
addNumbers(10.5, 20.5); // 30*
addNumbers('10.5', 20.5); // 30*
addNumbers(10, '20'); // 30
addNumbers('10', '20'); // 30
addNumbers('1sr0', '2sdf0');// TypeError: Argument #1 ($a) must be of type int, string given.

So, whenever the method addNumbersis called, PHP would expect to get the respective parameters of specified types (int).

When the function is called with an integer and a string, PHP, in the background, will try and convert the string to int and then perform the addition.
If PHP is not able to perform the conversion, will throw a fatal TypeError because the types do not match the expected integer types.

(*) In the case when we pass float to int-typed parameters, PHP will coerce values of the wrong type into the expected scalar type if possible.

addNumbers(10.5, 20.5) // 30
^ ^
10 20

addNumbers(10.5, '20.5') // 30
^ ^
10 20

Even the string representation of these numbers will work fine.
PHP will try to coerce “20.5” to its integer value which happens to be 20 in this case and process it further accordingly.

If we want to enable strict typing, you can use the declare(strict_types=1) directive at the beginning of a PHP file. This directive ensures that all type declarations are honored and type coercion is disabled.

<?php

declare(strict_types=1)

function addNumbers(int $a, int $b): int
{
return $a + $b;
}

addNumbers(10, 20); // 30
addNumbers(10.5, 20.5); // TypeError: Argument #1 ($a) must be of type int, float given.
addNumbers('10.5', 20.5); // TypeError: Argument #1 ($a) must be of type int, string given
addNumbers(10, '20'); // TypeError: Argument #2 ($b) must be of type int, string given.
addNumbers('10', '20'); // TypeError: Argument #1 ($a) must be of type int, string given.
addNumbers('1sr0', '2sdf0'); // TypeError: Argument #1 ($a) must be of type int, string given.

Caveats

One thing to note is that a function can be given an integer when it’s expecting a float value even in a strict typing context.
This is one exception in strict typing.

<?php

declare(strict_types=1)

function addNumbers(float $a, float $b): float
{
return $a + $b;
}

addNumbers(10, 20); // 30.0
addNumbers(10.5, 20.5); // 31.0
addNumbers(10.5, 20); // 30.5

This behavior is based on the assumption that integers can be safely converted to floats without any loss of precision or functionality.

Conclusion

Strict typing helps catch type-related errors early and promotes code clarity by explicitly defining the expected types. It can be particularly useful in preventing unintended type conversions that might lead to unexpected behavior.

By understanding the differences between loose typing and strict typing, PHP developers can choose the appropriate approach based on the specific requirements and desired level of type safety in their code.

Feel free to Subscribe for more content like this 🔔, clap 👏🏻 , comment 💬 and share the article with anyone you’d like

And as it always has been, I appreciate your support, and thanks for reading.

--

--