{"id":1423,"date":"2021-05-05T06:01:26","date_gmt":"2021-05-05T06:01:26","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1423"},"modified":"2025-04-06T07:15:51","modified_gmt":"2025-04-06T07:15:51","slug":"php-variable-functions","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-variable-functions\/","title":{"rendered":"PHP Variable Functions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you will learn about PHP variable functions and how to use them to call a function, an object&#8217;s method, and a class&#8217;s static method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-php-variable-functions'>Introduction to PHP variable functions <a href=\"#introduction-to-php-variable-functions\" class=\"anchor\" id=\"introduction-to-php-variable-functions\" title=\"Anchor for Introduction to PHP variable functions\">#<\/a><\/h2>\n\n\n\n<p>Variable functions allow you to use a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">variable<\/a> like a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-functions\/\">function<\/a>. When you append parentheses <code>()<\/code> to a variable, PHP will look for the function whose name is the same as the variable&#8217;s value and execute it. 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$f = <span class=\"hljs-string\">'strlen'<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $f(<span class=\"hljs-string\">'Hello'<\/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=PD9waHAKCiRmID0gJ3N0cmxlbic7CmVjaG8gJGYoJ0hlbGxvJyk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/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-number\">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>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define a variable <code>$f<\/code> and initialize its value to the <code>'strlen'<\/code> literal string.<\/li>\n\n\n\n<li>Second, use the <code>$f<\/code> as a function by passing the string <code>'Hello'<\/code> to it.<\/li>\n<\/ul>\n\n\n\n<p>When PHP sees <code>$f()<\/code>, it looks for the <code>strlen()<\/code> function. Because the <code>strlen()<\/code> is a built-in function, PHP just invokes it.<\/p>\n\n\n\n<p>If PHP cannot find the function name, it&#8217;ll raise an error. For example:<\/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$f = <span class=\"hljs-string\">'len'<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $f(<span class=\"hljs-string\">'Hello'<\/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=PD9waHAKCiRmID0gJ2xlbic7CmVjaG8gJGYoJ0hlbGxvJyk7\" 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-4\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Fatal error: Uncaught Error: Call to undefined function len() in index.php:5<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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>In this example, it issues an error because PHP cannot find the len() function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-variable-functions-to-call-a-method'>Using variable functions to call a method <a href=\"#using-variable-functions-to-call-a-method\" class=\"anchor\" id=\"using-variable-functions-to-call-a-method\" title=\"Anchor for Using variable functions to call a method\">#<\/a><\/h2>\n\n\n\n<p>The variable functions allow you to call the methods of an <a href=\"https:\/\/phptutorial.net\/php-oop\/php-objects\/\">object<\/a>. The syntax for calling a method using a variable function is as follows:<\/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\"><span class=\"hljs-keyword\">$this<\/span>-&gt;$variable($arguments)<\/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>Notice that you need to prefix the variable name with the <code>$<\/code> sign. In this case, you&#8217;ll have the <code>$<\/code> sign before the <code>this<\/code> keyword and the variable name. For example:<\/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-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Str<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $s;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">(string $s)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;s = $s;\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">lower<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> mb_strtolower(<span class=\"hljs-keyword\">$this<\/span>-&gt;s, <span class=\"hljs-string\">'UTF-8'<\/span>);\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">upper<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> mb_strtoupper(<span class=\"hljs-keyword\">$this<\/span>-&gt;s, <span class=\"hljs-string\">'UTF-8'<\/span>);\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">title<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> mb_convert_case(<span class=\"hljs-keyword\">$this<\/span>-&gt;s, MB_CASE_TITLE, <span class=\"hljs-string\">'UTF-8'<\/span>);\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(string $format)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">if<\/span> (!in_array($format, &#91;<span class=\"hljs-string\">'lower'<\/span>, <span class=\"hljs-string\">'upper'<\/span>, <span class=\"hljs-string\">'title'<\/span>])) {\n\t\t\t<span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-keyword\">Exception<\/span>(<span class=\"hljs-string\">'The format is not supported.'<\/span>);\n\t\t}\n\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">$this<\/span>-&gt;$format();\n\t}\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>How it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define a <code>Str<\/code> class that has three methods for converting a string to lowercase, uppercase, and title case.<\/li>\n\n\n\n<li>Second, define the <code>convert()<\/code> method that accepts a string. If the <code>format<\/code> argument is not one of the method names: lower, upper, and title, the <code>convert()<\/code> method will raise an exception. Otherwise, it&#8217;ll call the corresponding method <code>lower()<\/code>, <code>upper()<\/code> or <code>title()<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>The following shows how to use the <code>convert()<\/code> method of the <code>Str<\/code> class:<\/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<span class=\"hljs-keyword\">require_once<\/span> <span class=\"hljs-string\">'Str.php'<\/span>;\n\n$str = <span class=\"hljs-keyword\">new<\/span> Str(<span class=\"hljs-string\">'Hello there'<\/span>);\n\n<span class=\"hljs-keyword\">echo<\/span> $str-&gt;convert(<span class=\"hljs-string\">'title'<\/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>Output:<\/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\">Hello There<\/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<h3 class=\"wp-block-heading\" id='2-using-variable-functions-to-call-a-static-method-example'>2) Using variable functions to call a static method example <a href=\"#2-using-variable-functions-to-call-a-static-method-example\" class=\"anchor\" id=\"2-using-variable-functions-to-call-a-static-method-example\" title=\"Anchor for 2) Using variable functions to call a static method example\">#<\/a><\/h3>\n\n\n\n<p>The following example uses a variable function to call a <a href=\"https:\/\/phptutorial.net\/php-oop\/php-static-methods\/\">static method<\/a>:<\/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\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Str<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $s;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">(string $s)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;s = $s;\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__toString<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">$this<\/span>-&gt;s;\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">compare<\/span><span class=\"hljs-params\">(Str $s1, Str $s2)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> strcmp($s1, $s2);\n\t}\n}<\/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>The <code>Str<\/code> class has a constructor that accepts a string. It implements the <code>toString()<\/code> method that converts the <code>Str<\/code> instance to a string.<\/p>\n\n\n\n<p>The <code>Str<\/code> class has the <code>compare()<\/code> static method that compares two instances of the <code>Str<\/code> class. To call the <code>compare()<\/code> static method using a variable function, you use the following:<\/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\">$str1 = <span class=\"hljs-keyword\">new<\/span> Str(<span class=\"hljs-string\">'Hi'<\/span>);\n$str2 = <span class=\"hljs-keyword\">new<\/span> Str(<span class=\"hljs-string\">'Hi'<\/span>);\n\n$action = <span class=\"hljs-string\">'compare'<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> Str::$action($str1, $str2); <span class=\"hljs-comment\">\/\/ 0<\/span><\/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<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>Append parentheses () to a variable name to call the function whose name is the same as the variable&#8217;s value.<\/li>\n\n\n\n<li>Use the <code>$this->$variable()<\/code> to call a method of a class.<\/li>\n\n\n\n<li>Use the <code>className::$variable()<\/code> to call a static method of a class.<\/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=\"1423\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-variable-functions\/\"\n\t\t\t\tdata-post-title=\"PHP Variable Functions\"\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=\"1423\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-variable-functions\/\"\n\t\t\t\tdata-post-title=\"PHP Variable Functions\"\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 will learn about the PHP variable functions and how to use them to call a function, a method of an object, and a static method of a class.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":68,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1423","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1423","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=1423"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1423\/revisions"}],"predecessor-version":[{"id":3121,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1423\/revisions\/3121"}],"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=1423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}