php

What is Type Hinting in PHP?

PHP Type Hinting is a highly useful feature that has been available since PHP 5. Before this version, PHP functions did not require any specific data types to be declared in their arguments or parameters. With the introduction of PHP 5, developers can now specify data types for their PHP functions using a feature known as type hinting. This may be specifically beneficial when updating a specific instance within a PHP program.

In this article, we will delve into PHP’s type hinting capabilities and explore both weak and strict type hinting.

What is PHP Type Hinting?

Type hinting serves as a programming term that allows you to specify expected arguments’ data type in a function. For instance, when writing an “add” function, you can indicate that the parameter should be an integer. If you call the function with anything other than an integer, an error will occur, specifying that an integer is required. PHP offers two types of hints for scalar and return-type declarations:

What is Weak Type Hinting?

Weak type hinting is a default mode of operation for PHP, which does not produce any errors in case of type declaration mismatch. In other words, weak type hinting allows PHP to execute the code without generating an error, even if the data type of the parameter does not match the intended type.

Example

Consider an example that implements weak type hinting in PHP.

<?PHP
    $num1= 20;
    $num2= 3.16537;
    function multi(float $num1, int $num2)
    {
        return $num1 * $num2;
    }
    echo "The calculated value is: ",multi($num1, $num2);
?>

In the above code, the function multi takes two parameters, $num1 of type float and $num2 of type int, but the values passed as arguments are of different types. Despite the type mismatch, the code executes without generating an error or fatal error, which relates to the definition of weak type hinting.

Output

What is Strict Type Hinting?

Strict Type Hinting in PHP is a mode that enforces strict typing and throws a Fatal Error if a type declaration mismatch occurs. Only variables of the exact type of declaration type are accepted, and any other type will result in a Type Error.

To use strict type hinting, the first statement in the file must be (strict_types=1), or else a compiler error will occur. This mode only affects the specific file in which it is used and cannot be overridden at runtime, as it is entirely compile-time.

Example

Consider an example that implements strict type hinting in PHP.

<?PHP
    declare (strict_types=1);
    $num1= 20;
    $num2= 3.16537;
    function multi(float $num1, int $num2)
    {
        return $num1 * $num2;
    }
    echo "The calculated value is: ",multi($num1, $num2);
?>

The above example relates to strict type hinting as it includes the “declare (strict_types=1)” statement, which enables strict type checking in the file. Any type of declaration mismatches will result in a fatal error.

Output

The above code will give an error because the float value is being accessed using int and the integer value is being accessed using float data type.

Conclusion

Type hinting in PHP allows developers to specify expected arguments’ data types in a function. PHP offers two types of hints: weak type hinting and strict type hinting. Weak type hinting is a default mode of operation that does not produce any errors or fatal errors in case of type declaration mismatch, while strict type hinting enforces strict typing and throws a Fatal Error if a type declaration mismatch occurs. The developers should choose the appropriate type-hinting technique based on their specific requirements.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.