• Tutorial List

PHP Function Type Declaration (Type hint)

Last updated May 15, 2020
PHP Function Type Declaration

What does the type-declaration mean?

When you pass an argument inside a function, you can declare the type of the argument such as string, integer, boolean, etc.

And when you call the function then you have to pass the same specified type of value as the argument. If you don’t, you will get a type-error.


How to declare type in function arguments?

Syntax:

<?php
  	function myFunc(type $arg1, type $arg2){
    	//Code To Be Executed;
	}
?>

Example:

In the following example, we have declared integer (int) types in both arguments.

That’s why when you call this function, the value of the function arguments must be a number. Otherwise, you will get Type-Error.

<?php
  function myFunc(int $num1, int $num2){
      return $num1 + $num2;
  }

  $returnData = myFunc(21,30);
  echo $returnData;

  /*You can't do this
  $returnData = myFunc("Hello",30);
  */
?>
Browser Output
51

How to declare type in function return value?

You can also specify which type of value you want to return from a function.

Syntax:

<?php
  	function myFunc(type $arg) : return_type {
    	//Code To Be Executed;
	}
?>

Example:

<?php
  function myFunc(string $name) : string {
      return $name;
  }

  $returnData = myFunc("John Doe");
  echo $returnData;
?>
Browser Output
John Doe

Type Declaration in coercive & strict mode

👉 With Coercive mode

The coercive mode is the default behavior of the PHP type Declaration. This mode helps to return arguments or return value ​​according to the declared type.

Example:

In the following function, we declared the string type in the argument. But, when we call the function we pass an integer as the argument.

The Coercive mode converted the integer to a string. 25 to "25"

<?php
  function myFunc(string $val){
      return $val;
  }

  $returnData = myFunc(25);
  echo "$returnData <br>";

  // The PHP gettype() function is used to Get the type of a variable
  echo "$returnData is a ".gettype($returnData);
?>
Browser Output
25
25 is a string

👉 With the Strict mode

The strict mode is the opposite of the coercive mode. In strict mode, you must pass or return the same type of value that is declared. Otherwise, you will get the type-error.

To use the strict mode, first, you have to enable it –

<?php 
	declare(strict_types=1);
?>

Example:

<?php
  declare(strict_types=1);
  function myFunc(string $val){
      return $val;
  }

  /* You Can't do this
  $returnData = myFunc(25);
  */
  $returnData = myFunc("25");
  echo "$returnData <br>";
  echo "$returnData is an ".gettype($returnData);
?>
Browser Output
25
25 is an integer

What is PHP Scalar & Return type declaration?

  1. Scalar type declaration
    • When you declare a type on an argument, it is called a scalar type declaration.
  2. Return type declaration
    • When you declare a type on the return statement, it is called a return type declaration

Table of all the Types that are available in PHP

TypeDescriptionMinimum PHP version
Class/interface name The parameter must be an instance of the given class or interface name. PHP 5.0.0
self The parameter must be an instance of the same class as the one the method is defined on. This can only be used in class and instance methods. PHP 5.0.0
array The parameter must be an array PHP 5.1.0
callableThe parameter must be a valid callablePHP 5.4.0
boolThe parameter must be a boolean value.PHP 7.0.0
floatThe parameter must be a floating-point number.PHP 7.0.0
int The parameter must be an integer. PHP 7.0.0
stringThe parameter must be a string.PHP 7.0.0
iterableThe parameter must be either an array or an instance of Traversable.PHP 7.1.0
object The parameter must be an object. PHP 7.2.0