{"id":6958,"date":"2022-10-24T09:54:09","date_gmt":"2022-10-24T07:54:09","guid":{"rendered":"http:\/\/blog.bart.sk\/en\/?p=6958"},"modified":"2022-11-24T14:09:30","modified_gmt":"2022-11-24T13:09:30","slug":"functions-in-php","status":"publish","type":"post","link":"https:\/\/blog.bart.sk\/en\/functions-in-php\/","title":{"rendered":"Functions in PHP"},"content":{"rendered":"<p><b><\/b><b>In the previous article, we showed you how <\/b><b>to get PHP up and running<\/b><b> and presented some basic elements. In this section, we&#8217;d like to update your knowledge of the functions \u2013 how to create them, call them and process their output. We&#8217;ll describe classic functions, but also anonymous and the so-called arrow functions.<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Function is the basic building block of a program. It&#8217;s a &#8220;subprogram&#8221; that allows us to execute a sequence of commands. As a rule, a function receives several input arguments (or none) at the input and can return some value or just execute commands. Functions help us reuse the same code in multiple places (we can also call it the DRY principle \u2013 Don&#8217;t repeat yourself). This way you can clarify your program and divide it into smaller functional parts. We have built-in and user-defined functions.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">A function consists of several parts, namely:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>title (name) <\/b><span style=\"font-weight: 400;\">\u2013 name of the function that should describe its role \u2013 short and concise (see <\/span><a href=\"https:\/\/www.php.net\/manual\/en\/userlandnaming.rules.php\"><span style=\"font-weight: 400;\">PHP: Rules \u2013 Manual<\/span><\/a><span style=\"font-weight: 400;\">)<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>input arguments<\/b><span style=\"font-weight: 400;\"> \u2013 a list of input arguments that are necessary for the proper running of the function<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>function body<\/b><span style=\"font-weight: 400;\"> \u2013 a sequence of commands that perform the required task<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>return value<\/b><span style=\"font-weight: 400;\"> \u2013 the function can return an output value using the return command<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>function description<\/b><span style=\"font-weight: 400;\"> \u2013 description of the function, its parameters, return value<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">We can call the function only if it&#8217;s defined (created) before the call, i.e. our program already knows it. Use the keyword &#8220;<\/span><b>function<\/b><span style=\"font-weight: 400;\">&#8221; to create a function.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">An example of a function that does nothing. It has a name, no input arguments, an empty body, it doesn&#8217;t return anything (no return value):<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\nfunction nothing()\r\n{}<\/pre>\n<p><span style=\"font-weight: 400;\">or we can try something meaningful, e.g.:<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> content_of_circle_using_radius<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$radius<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">{<\/span><span style=\"font-weight: 400;\">\u00a0<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">return<\/span> <i><span style=\"font-weight: 400;\">M_PI <\/span><\/i><span style=\"font-weight: 400;\">*<\/span><span style=\"font-weight: 400;\"> $radius <\/span><span style=\"font-weight: 400;\">**<\/span> <span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">This function has a name, an input argument, a body and a return value. The function defined in this way (as the name implies) returns the area of the circle according to the specified argument (it should be a radius). We can reasonably deduce that from the definition.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">However, there may be situations created in the program when the function doesn&#8217;t return the expected result. E.g. if <\/span><b>$radius = \u2018xXx\u2019; <\/b><span style=\"font-weight: 400;\">at best it throws out an<\/span><b> exception <\/b><span style=\"font-weight: 400;\">(from php v8.0) or returns a <\/span><b>0.<\/b><span style=\"font-weight: 400;\"> What about the return value? In this case, it&#8217;s still a <\/span><b>float<\/b><span style=\"font-weight: 400;\">, but if we don&#8217;t know (see) the body of the function, it could be anything. Therefore, we shouldn&#8217;t rely on <\/span><b>float<\/b><span style=\"font-weight: 400;\">, but we should also treat it after calling the function so that the program behaves as expected.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This can be avoided a little and the definition of the function can be supplemented by the types of input arguments and the type of return value \u2013 this can be described in the function description. In order for the interpreter to verify and evaluate it for us, we turn on the so-called <\/span><b>strict mode<\/b><span style=\"font-weight: 400;\">. Insert <\/span><b>declare(strict_types=1)<\/b><span style=\"font-weight: 400;\"> at the beginning of the file.<\/span> <span style=\"font-weight: 400;\">In strict mode, PHP expects values with the same type as the target types. If a mismatch occurs, PHP invokes an exception (<\/span><b>TypeError<\/b><span style=\"font-weight: 400;\">). Like this:<\/span><b>\u00a0<\/b><\/p>\n<pre>&lt;?php\r\ndeclare(strict_types=1);\r\n<i>\/**<\/i>\r\n<i>* @param float $radius radius, should be positive value<\/i>\r\n<i>*<\/i>\r\n<i>* @return float<\/i>\r\n<i>*\/<\/i>\r\nfunction content_of_circle_using_radius(float $radius): float\r\n{\r\n\u00a0\u00a0\u00a0return <i>M_PI <\/i>* $r ** 2;\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Now we know by definition that the argument must be a <\/span><b>float <\/b><span style=\"font-weight: 400;\">and the return value will also be a <\/span><b>float.<\/b><span style=\"font-weight: 400;\"> We can certainly rely on that now.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When creating functions, it&#8217;s sometimes advisable to set the<\/span><b> default<\/b><span style=\"font-weight: 400;\"> value of the argument. This is used mainly if we assume that the argument will be the same in most cases. To define the default value, we simply assign it to a variable.<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">declare<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">strict_types<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> how_you_feel<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">string<\/span><span style=\"font-weight: 400;\"> $name<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\"> $age <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">):<\/span> <span style=\"font-weight: 400;\">string<\/span>\r\n<span style=\"font-weight: 400;\">{<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> $name <\/span><span style=\"font-weight: 400;\">.<\/span> <span style=\"font-weight: 400;\">\" feels \"<\/span> <span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\"> $age <\/span><span style=\"font-weight: 400;\">.<\/span> <span style=\"font-weight: 400;\">\" years old\"<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n<span style=\"font-weight: 400;\">how_you_feel<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"Tomas\"<\/span><span style=\"font-weight: 400;\">);<\/span> <span style=\"font-weight: 400;\">\/\/ returns Tomas feels 25 years old<\/span>\r\n<span style=\"font-weight: 400;\">how_you_feel<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"Viktor\"<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">43<\/span><span style=\"font-weight: 400;\">);<\/span> <span style=\"font-weight: 400;\">\/\/ returns Viktor feels 43 years old<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">This actually tells us that the argument is optional and we can omit it when calling the function. Here, however, it should be noted that optional arguments, or arguments with default values, should be placed after the mandatory arguments. Since PHP v8.0.0, it&#8217;s <\/span><b>deprecated<\/b><span style=\"font-weight: 400;\"> to declare optional arguments before mandatory ones. However, there&#8217;s an exception \u2013 if an optional argument has a specified type and has a declared default value of <\/span><i><span style=\"font-weight: 400;\">null<\/span><\/i><span style=\"font-weight: 400;\">, then it&#8217;s possible to place it before the mandatory argument. So it&#8217;s essentially necessary to enter it when calling, but we can use a <\/span><i><span style=\"font-weight: 400;\">null value.<\/span><\/i><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">declare<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">strict_types<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$bar<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> $baz<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{}<\/span> <span style=\"font-weight: 400;\">\/\/ all ok<\/span>\r\n<span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$bar<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> $baz <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{}<\/span> <span style=\"font-weight: 400;\">\/\/ all ok<\/span>\r\n<span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$bar <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> $baz<\/span><span style=\"font-weight: 400;\">){}<\/span> <span style=\"font-weight: 400;\">\/\/ deprecated as of PHP v8.0.0<\/span>\r\n<span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\"> $bar <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> $baz<\/span><span style=\"font-weight: 400;\">){}<\/span> <span style=\"font-weight: 400;\">\/\/ all ok<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">When declaring a function, we can also use &#8220;&#8230;&#8221;, the so-called spread operator. This notation tells us that a function can have a variable number of arguments.<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">declare<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">strict_types<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$bar<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">...<\/span><span style=\"font-weight: 400;\">$baz<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{}<\/span>\r\n<span style=\"font-weight: 400;\">foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">);<\/span> <span style=\"font-weight: 400;\">\/\/ valid call<\/span>\r\n<span style=\"font-weight: 400;\">foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">);<\/span> <span style=\"font-weight: 400;\">\/\/ valid call<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">The <\/span><b>$baz <\/b><span style=\"font-weight: 400;\">argument is a field of arguments, i.e. in the function body, the argument can be scrolled as a classic field, e.g. by <\/span><i><span style=\"font-weight: 400;\">foreach<\/span><\/i><span style=\"font-weight: 400;\">. We can even set the type for the given variable arguments,\u00a0<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">declare<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">strict_types<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$bar<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">int<\/span> <span style=\"font-weight: 400;\">...<\/span><span style=\"font-weight: 400;\">$baz<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{}<\/span>\r\n<span style=\"font-weight: 400;\">foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">);<\/span> <span style=\"font-weight: 400;\">\/\/ valid call<\/span>\r\n<span style=\"font-weight: 400;\">foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">);<\/span> <span style=\"font-weight: 400;\">\/\/ valid call<\/span>\r\n<span style=\"font-weight: 400;\">foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">\"3\"<\/span><span style=\"font-weight: 400;\">);<\/span> <span style=\"font-weight: 400;\">\/\/ raise TypeError<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">&#8220;&#8230;&#8221; can be used not only in defining the function, but also in calling.<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">declare<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">strict_types<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$bar<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> $baz<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{}<\/span>\r\n<span style=\"font-weight: 400;\">$args <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">];<\/span>\r\n<span style=\"font-weight: 400;\">foo<\/span><span style=\"font-weight: 400;\">(...<\/span><span style=\"font-weight: 400;\">$args<\/span><span style=\"font-weight: 400;\">);<\/span> <span style=\"font-weight: 400;\">\/\/ $bar = 1, $baz = 2<\/span><\/pre>\n<p>Until now, all function definitions had to strictly follow the order of arguments when calling. If, for example, we have a function with three optional parameters and we want to only change the last one, we have to set the other ones too.<\/p>\n<p><span style=\"font-weight: 400;\">But that changed with the arrival of PHP v8.0. Named arguments were introduced as an extension of existing position parameters. Named arguments allow you to pass arguments to a function based on the parameter name and not based on the position of the parameter. This makes the arguments independent of the order and allows you to arbitrarily skip the default values.<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">declare<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">strict_types<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> foo<\/span><span style=\"font-weight: 400;\">(<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\"> $bar <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\"> $baz <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">string<\/span><span style=\"font-weight: 400;\"> $qux <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">''<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">string<\/span><span style=\"font-weight: 400;\"> $quux <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">''<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{}<\/span>\r\n<span style=\"font-weight: 400;\">foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">qux<\/span><span style=\"font-weight: 400;\">:<\/span> <span style=\"font-weight: 400;\">'some string'<\/span><span style=\"font-weight: 400;\">);<\/span> <span style=\"font-weight: 400;\">\/\/ named argument<\/span>\r\n<span style=\"font-weight: 400;\">\/\/ $bar = null, $baz = 1, qux = 'some string', $quux = ''<\/span>\r\n<span style=\"font-weight: 400;\">foo<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">'some string'<\/span><span style=\"font-weight: 400;\">);<\/span> <span style=\"font-weight: 400;\">\/\/ old style<\/span>\r\n<span style=\"font-weight: 400;\">\/\/ $bar = null, $baz = 1, qux = 'some string', $quux = ''<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Since version PHP v8.1 we can also use &#8220;\u2026&#8221; (spread operator). The order of arguments in an array doesn&#8217;t matter.<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">declare<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">strict_types<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">$args <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">[<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">'baz'<\/span> <span style=\"font-weight: 400;\">=&gt;<\/span> <span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">'bar'<\/span> <span style=\"font-weight: 400;\">=&gt;<\/span> <span style=\"font-weight: 400;\">4<\/span>\r\n<span style=\"font-weight: 400;\">];<\/span>\r\n<span style=\"font-weight: 400;\">foo<\/span><span style=\"font-weight: 400;\">(...<\/span><span style=\"font-weight: 400;\">$args<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> quux<\/span><span style=\"font-weight: 400;\">:<\/span> <span style=\"font-weight: 400;\">'some string'<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">\/\/ $bar = 4, $baz = 2, qux = '', $quux = 'some string'<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">We talked about how to pass arguments to a function. It was with a value, a default value, a spread operator and named arguments. The last option is passing arguments using a reference. This means that if we change the argument in the body of the function, the argument outside the body of the function itself changes in the parent scope. Just add the ampersand (&amp;) character before the argument in the function definition.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">By default, PHP passes arguments by value, but if the argument is an instance of an object, it is passed by reference.<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">declare<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">strict_types<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">$a <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> modify<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">int<\/span> <span style=\"font-weight: 400;\">&amp;<\/span><span style=\"font-weight: 400;\">$a<\/span><span style=\"font-weight: 400;\">):<\/span> <span style=\"font-weight: 400;\">int<\/span> <span style=\"font-weight: 400;\">{<\/span> <span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> $a<\/span><span style=\"font-weight: 400;\">++;<\/span> <span style=\"font-weight: 400;\">}<\/span>\r\n<span style=\"font-weight: 400;\">$result <\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\"> modify<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$a<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">echo $result<\/span><span style=\"font-weight: 400;\">;<\/span> <span style=\"font-weight: 400;\">\/\/ 1<\/span>\r\n<span style=\"font-weight: 400;\">echo $a<\/span><span style=\"font-weight: 400;\">;<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">\/\/ 2<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">In the introduction, we stated that a function consists of several parts. One of them is the name. The function must have a name if we want to call it. Reasonable, but there are also so-called anonymous functions. They have everything that was mentioned, but they don&#8217;t have a name. They&#8217;re known as <\/span><i><span style=\"font-weight: 400;\">closures.<\/span><\/i><span style=\"font-weight: 400;\"> They&#8217;re mainly used as callable <\/span><i><span style=\"font-weight: 400;\">arguments<\/span><\/i><span style=\"font-weight: 400;\"> in a function. But they also have other uses as they are an implementation of the <\/span><a href=\"https:\/\/www.php.net\/manual\/en\/class.closure.php\"><span style=\"font-weight: 400;\">Closure<\/span><\/a><span style=\"font-weight: 400;\"> class.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We can use them as a callable argument, for example, in the usort function. This will sort the input field by return value from the anonymous function.<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">declare<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">strict_types<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">$arr <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">[<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">'baz'<\/span> <span style=\"font-weight: 400;\">=&gt;<\/span> <span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">'bar'<\/span> <span style=\"font-weight: 400;\">=&gt;<\/span> <span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">'qux'<\/span> <span style=\"font-weight: 400;\">=&gt;<\/span> <span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">];<\/span>\r\n<span style=\"font-weight: 400;\">usort<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$arr<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$a<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> $b<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">if<\/span> <span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$a <\/span><span style=\"font-weight: 400;\">===<\/span><span style=\"font-weight: 400;\"> $b<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">return<\/span> <span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">return<\/span> <span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$a <\/span><span style=\"font-weight: 400;\">&lt;<\/span><span style=\"font-weight: 400;\"> $b<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">?<\/span> <span style=\"font-weight: 400;\">-<\/span><span style=\"font-weight: 400;\">1<\/span> <span style=\"font-weight: 400;\">:<\/span> <span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Anonymous functions have their own local scope, so it isn&#8217;t possible to use a variable outside the body of the function. If we still need to access a variable defined outside the body of the function, we use the <\/span><b>use <\/b><span style=\"font-weight: 400;\">operator.<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">declare<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">strict_types<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">$message <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">\"I am compare %s and %s\"<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">PHP_EOL<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">$anonym <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$a<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> $b<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">use<\/span> <span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$message<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0echo sprintf<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$message<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> $a<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> $b<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">if<\/span> <span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$a <\/span><span style=\"font-weight: 400;\">===<\/span><span style=\"font-weight: 400;\"> $b<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">return<\/span> <span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">return<\/span> <span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$a <\/span><span style=\"font-weight: 400;\">&lt;<\/span><span style=\"font-weight: 400;\"> $b<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">?<\/span> <span style=\"font-weight: 400;\">-<\/span><span style=\"font-weight: 400;\">1<\/span> <span style=\"font-weight: 400;\">:<\/span> <span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">};<\/span>\r\n<span style=\"font-weight: 400;\">$arr <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">];<\/span>\r\n<span style=\"font-weight: 400;\">usort<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$arr<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> $anonym<\/span><span style=\"font-weight: 400;\">);<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">If we needed a simple function, for example, to multiply each element of a field by a constant, we could use the so-called <\/span><b>arrow function<\/b><span style=\"font-weight: 400;\">. It&#8217;s defined simply by <\/span><b>fn(arguments) =&gt; expr. <\/b><span style=\"font-weight: 400;\">It has the same functionality as the anonymous function, but we can use the variables from the parent scope.\u00a0<\/span><\/p>\n<pre><b>&lt;?<\/b><b>php<\/b>\r\n<span style=\"font-weight: 400;\">declare<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">strict_types<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">$arr <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">];<\/span>\r\n<span style=\"font-weight: 400;\">$multiplier <\/span><span style=\"font-weight: 400;\">=<\/span> <span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">$result <\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\"> array_map<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">fn<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$x<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">=&gt;<\/span><span style=\"font-weight: 400;\"> $x <\/span><span style=\"font-weight: 400;\">*<\/span><span style=\"font-weight: 400;\"> $multiplier<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> $arr<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">\/\/ same as\u00a0<\/span>\r\n<span style=\"font-weight: 400;\">$result <\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\"> array_map<\/span><span style=\"font-weight: 400;\">(<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$x<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">use<\/span> <span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">$multiplier<\/span><span style=\"font-weight: 400;\">)<\/span> <span style=\"font-weight: 400;\">{<\/span><span style=\"font-weight: 400;\">\u00a0<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> $x <\/span><span style=\"font-weight: 400;\">*<\/span><span style=\"font-weight: 400;\"> $multiplier<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">},<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$arr<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">);<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">The next blog in our PHP series will be dedicated to generators. Until then, good luck coding ?<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"In the previous article, we showed you how to get PHP up and running and presented some basic&hellip;","protected":false},"author":42,"featured_media":6962,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","csco_display_header_overlay":false,"csco_singular_sidebar":"","csco_page_header_type":""},"categories":[199,211],"tags":[331,332,293],"_links":{"self":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/6958"}],"collection":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/comments?post=6958"}],"version-history":[{"count":1,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/6958\/revisions"}],"predecessor-version":[{"id":6959,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/6958\/revisions\/6959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/media\/6962"}],"wp:attachment":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/media?parent=6958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/categories?post=6958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/tags?post=6958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}