{"id":2264,"date":"2021-07-11T03:12:00","date_gmt":"2021-07-11T03:12:00","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2264"},"modified":"2025-04-07T13:43:35","modified_gmt":"2025-04-07T13:43:35","slug":"php-type-hints","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-type-hints\/","title":{"rendered":"PHP Type Hints"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use PHP type hints to declare the types for function parameters and return values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-php-type-hints'>Introduction to PHP type hints <a href=\"#introduction-to-php-type-hints\" class=\"anchor\" id=\"introduction-to-php-type-hints\" title=\"Anchor for Introduction to PHP type hints\">#<\/a><\/h2>\n\n\n\n<p>PHP is a dynamically typed language. When you <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-functions\/\">define a function<\/a>, you don&#8217;t need to declare types for <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-function-parameters\/\">parameters<\/a>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">add<\/span><span class=\"hljs-params\">($x, $y)<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> $x + $y;\n}\n\n$result = add(<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $result; <span class=\"hljs-comment\">\/\/ 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmZ1bmN0aW9uIGFkZCgkeCwgJHkpCnsKICAgIHJldHVybiAkeCArICR5Owp9CgokcmVzdWx0ID0gYWRkKDEsMik7CmVjaG8gJHJlc3VsdDsgLy8gMw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The <code>add()<\/code> function accepts two arguments and returns the sum of them. In this example, we pass two integers into the <code>add()<\/code> function and get the result as an integer.<\/p>\n\n\n\n<p>If you pass two floating-point numbers into the <code>add()<\/code> function, you&#8217;ll get the sum of the <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-float\/\">floats<\/a>, which is a floating-point number:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">add<\/span><span class=\"hljs-params\">($x, $y)<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> $x + $y;\n}\n\n$result = add(<span class=\"hljs-number\">1.0<\/span>,<span class=\"hljs-number\">2.5<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $result; <span class=\"hljs-comment\">\/\/ 3.5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmZ1bmN0aW9uIGFkZCgkeCwgJHkpCnsKICAgIHJldHVybiAkeCArICR5Owp9CgokcmVzdWx0ID0gYWRkKDEuMCwyLjUpOwplY2hvICRyZXN1bHQ7IC8vIDMuNQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>More interestingly, you can pass an integer and a numeric string into the <code>add()<\/code> function, it will return an integer:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">add<\/span><span class=\"hljs-params\">($x, $y)<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> $x + $y;\n}\n\n$result = add(<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-string\">'2'<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $result; <span class=\"hljs-comment\">\/\/ 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmZ1bmN0aW9uIGFkZCgkeCwgJHkpCnsKICAgIHJldHVybiAkeCArICR5Owp9CgokcmVzdWx0ID0gYWRkKDEsJzInKTsKZWNobyAkcmVzdWx0OyAvLyAz\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this case, the <code>add()<\/code> function implicitly coerces the numeric string <code>'2'<\/code> into the integer <code>2<\/code> because of the <code>+<\/code> operator. <\/p>\n\n\n\n<p>If PHP fails to coerce the string argument into an integer, it&#8217;ll issue an error. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">add<\/span><span class=\"hljs-params\">($x, $y)<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> $x + $y;\n}\n\n$result = add(<span class=\"hljs-string\">'Hi'<\/span>,<span class=\"hljs-string\">'There'<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $result;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmZ1bmN0aW9uIGFkZCgkeCwgJHkpCnsKICAgIHJldHVybiAkeCArICR5Owp9CgokcmVzdWx0ID0gYWRkKCdIaScsJ1RoZXJlJyk7CmVjaG8gJHJlc3VsdDs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Fatal error: Uncaught TypeError: Unsupported operand types: string + string in ...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can use type hints to enforce the types for function parameters and return values.<\/p>\n\n\n\n<p class=\"note\">PHP also allows you to use type hints for class properties and methods which you&#8217;ll learn in the <a href=\"https:\/\/phptutorial.net\/php-oop\/\">PHP object-oriented programming tutorial<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-type-hints-for-function-parameters'>PHP type hints for function parameters <a href=\"#php-type-hints-for-function-parameters\" class=\"anchor\" id=\"php-type-hints-for-function-parameters\" title=\"Anchor for PHP type hints for function parameters\">#<\/a><\/h2>\n\n\n\n<p>The type hints ensure that PHP will check the type of a value at the call time and throw a <code>TypeError<\/code> if there is a mismatch.<\/p>\n\n\n\n<p>To add a type hint to a parameter, you place a type in front of it like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">my_function<\/span><span class=\"hljs-params\">(type $param1, type param2, ...)<\/span> <\/span>{\n   <span class=\"hljs-comment\">\/\/ ...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In PHP 5, you can use <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array\/\">array<\/a><\/code>, <code>callable<\/code>, and <a href=\"https:\/\/phptutorial.net\/php-oop\/php-objects\/\">class<\/a> for type hints. In PHP 7+, you can also use scalar types such as <code>bool<\/code>, <code>float<\/code>, <code>int<\/code>, and <code>string<\/code>.<\/p>\n\n\n\n<p>The following defines the <code>add()<\/code> function that accepts two integers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">add<\/span><span class=\"hljs-params\">(int $x, int $y)<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> $x + $y;\n}\n\n$result = add(<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $result;  <span class=\"hljs-comment\">\/\/ 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmZ1bmN0aW9uIGFkZChpbnQgJHgsIGludCAkeSkKewogICAgcmV0dXJuICR4ICsgJHk7Cn0KCiRyZXN1bHQgPSBhZGQoMSwyKTsKZWNobyAkcmVzdWx0OyAgLy8gMw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>However, if you pass two floats, you&#8217;ll get the result as an integer:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">add<\/span><span class=\"hljs-params\">(int $x, int $y)<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> $x + $y;\n}\n\n$result = add(<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2.5<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $result; <span class=\"hljs-comment\">\/\/ 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this case, PHP implicitly coerces <code>2.5<\/code> into an integer (<code>2<\/code>) before calculating the sum. Therefore, the result is an integer.<\/p>\n\n\n\n<p>Note that PHP issued the following decprecation message:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Deprecated: Implicit conversion from float <span class=\"hljs-number\">2.5<\/span> to int loses precision<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By default, PHP coerces a value of the compatible type into the expected scalar type declaration if possible. To enforce the value with a type that matches the type declaration, you must <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-strict_types\/\">declare strict typing<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-type-hints-for-the-functions-return-value'>PHP type hints for the function&#8217;s return value <a href=\"#php-type-hints-for-the-functions-return-value\" class=\"anchor\" id=\"php-type-hints-for-the-functions-return-value\" title=\"Anchor for PHP type hints for the function&#039;s return value\">#<\/a><\/h2>\n\n\n\n<p>To specify a return value&#8217;s type for a function, you add the type after the function header like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">my_function<\/span><span class=\"hljs-params\">(type $param1, type $param2, ...)<\/span> : <span class=\"hljs-title\">type<\/span> \n<\/span>{\n    <span class=\"hljs-comment\">\/\/ ..<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example defines the <code>add()<\/code> function that accepts two integers and returns an integer:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">add<\/span><span class=\"hljs-params\">(int $x, int $y)<\/span>: <span class=\"hljs-title\">int<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> $x + $y;\n}\n\n<span class=\"hljs-keyword\">echo<\/span> add(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmZ1bmN0aW9uIGFkZChpbnQgJHgsIGludCAkeSk6IGludAp7CiAgICByZXR1cm4gJHggKyAkeTsKfQoKZWNobyBhZGQoMTAsIDIwKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Starting from PHP 7.0, if a function doesn&#8217;t return a value, you use the <code>void<\/code> type. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">dd<\/span><span class=\"hljs-params\">($data)<\/span>:<span class=\"hljs-title\">void<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'&lt;pre&gt;'<\/span>;\n    var_dump($data);\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'&lt;\/pre&gt;'<\/span>;\n    <span class=\"hljs-keyword\">die<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='the-union-type'>The union type <a href=\"#the-union-type\" class=\"anchor\" id=\"the-union-type\" title=\"Anchor for The union type\">#<\/a><\/h2>\n\n\n\n<p>Starting from PHP 8.0, if a function returns a value of several types, you can declare it as a union type. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">add<\/span><span class=\"hljs-params\">($x, $y)<\/span>: <span class=\"hljs-title\">int<\/span> | <span class=\"hljs-title\">float<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> $x * $y;\n}\n\nvar_dump(add(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>));\nvar_dump(add(<span class=\"hljs-number\">1.5<\/span>, <span class=\"hljs-number\">2.5<\/span>));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmZ1bmN0aW9uIGFkZCgkeCwgJHkpOiBpbnQgfCBmbG9hdAp7CiAgICByZXR1cm4gJHggKiAkeTsKfQoKdmFyX2R1bXAoYWRkKDEwLCAyMCkpOwp2YXJfZHVtcChhZGQoMS41LCAyLjUpKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, the <code>add()<\/code> function returns an integer or a floating-point number, depending on the types of arguments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-mixed-type'>The mixed type <a href=\"#the-mixed-type\" class=\"anchor\" id=\"the-mixed-type\" title=\"Anchor for The mixed type\">#<\/a><\/h2>\n\n\n\n<p>If a function returns a value of many types, you can use the <code>mixed<\/code> type. The <code>mixed<\/code> type means one of several types. The <code>mixed<\/code> type is equivalent to the following union type:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">object|resource|<span class=\"hljs-keyword\">array<\/span>|string|int|float|bool|<span class=\"hljs-keyword\">null<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"note\">The <code>mixed<\/code> has been available since PHP 8.0.0.<\/p>\n\n\n\n<p>For example, the built-in function <code>filter_var()<\/code> uses both union type (<code>array|int<\/code>) and <code>mixed<\/code> type as the type hints:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">filter_var(mixed $value, int $filter = FILTER_DEFAULT, <span class=\"hljs-keyword\">array<\/span>|int $options = <span class=\"hljs-number\">0<\/span>): mixed<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='the-nullable-type'>The nullable type <a href=\"#the-nullable-type\" class=\"anchor\" id=\"the-nullable-type\" title=\"Anchor for The nullable type\">#<\/a><\/h2>\n\n\n\n<p>The following defines a function that accepts a string and returns the uppercase of that string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">upper<\/span><span class=\"hljs-params\">(string $str)<\/span>: <span class=\"hljs-title\">string<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> strtoupper($str);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you pass an argument of null, you&#8217;ll get an error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">upper<\/span><span class=\"hljs-params\">(string $str)<\/span>: <span class=\"hljs-title\">string<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> strtoupper($str);\n}\n\n$str = <span class=\"hljs-keyword\">null<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> upper($str);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Fatal error: Uncaught TypeError: Argument 1 passed to upper() must be of the type string, null given<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To fix this, you can make the <code>$str<\/code> parameter nullable like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">upper<\/span><span class=\"hljs-params\">(?string $str)<\/span>: ?<span class=\"hljs-title\">string<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">if<\/span>($str != <span class=\"hljs-keyword\">null<\/span>) {\n        <span class=\"hljs-keyword\">return<\/span> strtoupper($str);\n    }\n\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">null<\/span>;\n         \n}\n\n$str = <span class=\"hljs-keyword\">null<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> upper($str);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmZ1bmN0aW9uIHVwcGVyKD9zdHJpbmcgJHN0cik6ID9zdHJpbmcKewogICAgaWYoJHN0ciAhPSBudWxsKSB7CiAgICAgICAgcmV0dXJuIHN0cnRvdXBwZXIoJHN0cik7CiAgICB9CgogICAgcmV0dXJuIG51bGw7CiAgICAgICAgIAp9Cgokc3RyID0gbnVsbDsKZWNobyB1cHBlcigkc3RyKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p class=\"note\">The nullable type was introduced in PHP 7.1.<\/p>\n\n\n\n<p>PHP allows you to mark the type declarations and returns values as nullable by prefixing the type name with a question mark (<code>?<\/code>).<\/p>\n\n\n\n<p>In the above example, we add the <code>?<\/code> to the string type of the <code>$str<\/code> parameter. The <code>?string<\/code> allows you to pass a string argument or null. <\/p>\n\n\n\n<p>We also mark the return type <code>string<\/code> with <code>?<\/code> to make the type of the return value nullable.<\/p>\n\n\n\n<p>Note that the <code>mixed<\/code> type already includes the null type. Therefore, you don&#8217;t need to include nullable mixed like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">? mixed<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you do this, you&#8217;ll encounter an error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use PHP type hints for function parameters and return types.<\/li>\n\n\n\n<li>Use <code>void<\/code> type if the function doesn&#8217;t return any value.<\/li>\n\n\n\n<li>Use <code>mixed<\/code> type or union type if function parameter or function return value expect several types.<\/li>\n\n\n\n<li>To make a type nullable, prefix the type with a question mark (<code>?type<\/code>).<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2264\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-type-hints\/\"\n\t\t\t\tdata-post-title=\"PHP Type Hints\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2264\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-type-hints\/\"\n\t\t\t\tdata-post-title=\"PHP Type Hints\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about PHP type hints that declare the type for function parameters and return value.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":38,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2264","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2264"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2264\/revisions"}],"predecessor-version":[{"id":3275,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2264\/revisions\/3275"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}