The PHP type hinting can be defined as the expected values of PHP data types that can be passed into the PHP arguments.

When you define a function in PHP you don’t need to specify the data type of parameters because PHP is typed language, It is adding the data types as a dynamic when calling the function with its arguments.

An Overview

The functions in any programming language such as C#, C++ and others will not work before specifying the data type in the parameters.

The following type hint is an example for a function in a C++ language.

int people_counters( string name, int string ) {
  // ... Your Code here 
  // return integer data
}

So when you call this function in another place you have to specify the arguments with the same data type otherwise it will show you an error.

But in PHP, it is an optional, you can write it or you can exclude it from the parameters. For example

<?php 

  function write_me( $array ) {
    print_r( $array );
  }

?>

Or you can specify the data type in the parameters.

<?php 

  function write_me( array $array ) {
    print_r( $array );
  }

?>

But here you have to pass an array as a data type.

<?php 
  write_me([
     "name" => "codedtag",
     "age"  => 35
  ]);

This will print the full array.

Let’s focus how the type hinting works on the sage.

PHP Type Hinting in the Function Parameters

To add type hint for function parameters you have to add the type beside the variable and when you invoke this function you need to pass the same data types for all values to avoid the following error.

Fatal error: Uncaught TypeError: func(): Argument #1 ($xyz) must be of type int, string given, called in index.php

The basic structure for function with type hint would be like the below.

function exec__( TYPE $param, TYPE $param ) {
  // you code here
}

Let’s see a real example.

<?php
  $f = "Username data";
  $a = 125;
  function print_value( string $string, int $integer ) {
    echo $string;
    echo "<br />";
    echo $integer;
  }
  print_value($f, $a);
?>

The output would be like the below.

Username data
125

if you are working with arithmetic operators. The PHP type hint helping us to be more sensitive when writing function values . So you will need to pass the exact data type values to avoid any expected error.

PHP Type Hinting with Function’s Return

If you see the above example related c++, you will see all functions has already a type hint, and you will not able to write any function without types in the c++ language. Such as: int functionName, void functionName.

PHP allow you do the same job but here the structure different than it.

function FunctionName() : TYPE_HERE {
  // it should return the same data type
}

If the function doesn’t return the exact data type or doesn’t return anything, it will produce an error like the below.

Fatal error: Uncaught TypeError: func(): Return value must be of type int, none returned in index.php

Note that: the function will execute the code bock but it will show you the fatal error if it doesn’t return the same type.

For example.

<?php
  function getUserName(): array {
     $array = [
       "name" => "Peter", 
       "age" => "35 years",
       "site" => "CodedTag.com"
     ];
     
     return $array;
  }
   
  $arr = getUserName();
  print_r($arr); 
?>

This will print

Another example for integer type.

<?php
  function multiplication( int $a, int $b ): int {
   $c = $a * $b;
   return $c;
  }

  $calcs = multiplication(12, 12);
  echo $calcs; // 144
?>

Example for string float type.

<?php
  function calc( float $a, int $b ): float {
    $c = $a + $b;
    return $c;
  }

  $calcs = calc(5.5, 12);
  echo $calcs; // 17.5
?>

PHP Union Types

The PHP union type allows the function to return more than type. The PHP union type takes this operator “|” between types. The expression would be as the following.

<?php

  function union_type_func( int $val1, float $val2 ) : int | float {
    return $val1 - $val2;
  }

  echo union_type_func(11, 5.25); // 5.75 => Float 
  echo union_type_func(500, 120); // 380 => Integer

?>

You can add more than one type in the function such as int | float | string | array and so on. But note that, the union type only allowed in PHP8 and above. The other versions will show you the following error.

Parse error: syntax error, unexpected '|', expecting '{' in /index.php on line 2

The Nullable Type

Sometimes, when you pass a null data type into a string union function it occurs an error. For example.

<?php

  function string_fun( string $data ) : string {
    return strlen($data);
  }
  
  echo string_fun(null);
  
?>

This example should show you the string length, but it will show

Fatal error: Uncaught TypeError: Argument 1 passed to string_fun() must be of the type string, null given, called in index.php

To avoid this issue, you have to use “?“ beside the parameter data type. Like the below example.

<?php

  function string_fun( ?string $data ) : string {
    return strlen($data);
  }
  
  echo string_fun(null);
  
?>

This will avoid the error.

PHP Type Hinting with Return Only Types

In PHP 7.1 there is another feature has been added, the void keyword with the function which means there is no return in this function.

The basic syntax would be written like the below.

<?php

  function printing( string $data ) : void{
    echo "Printed";
  }
  
  echo printing();
  
?>

Wrapping UP

In this tutorial, you understood how to pass a specific data type into a PHP function and also learned how the nullable type works with ”?” operator.

Also, you saw examples for union types, and how to return a specific type from a function has a specific data type.