In this tutorial, you will learn how to declare types in PHP function parameters and return values.

What the mean of the type declaration?

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:

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:

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

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

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?

What is PHP Scalar type declaration?

When you declare a type on an argument, it is called a scalar type declaration.

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

Original Source

Try these yourself

TypeDescriptionMinimum PHP version
Class/interface name The parameter must be an instanceof the given class or interface name. PHP 5.0.0
self The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on 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 instanceof Traversable.PHP 7.1.0
object The parameter must be an object. PHP 7.2.0

Leave a Reply

Your email address will not be published. Required fields are marked *