The variable function refers to the variable that has parentheses ( .. ) and assign a function name in PHP. That leads the PHP translator to check if this variable is storing a callback or a callable function. Otherwise, it will produce an error.

Let’s see examples.

How to Assign a Function to Variable using PHP

As I mentioned, the variable function concept allows you to put the name of the callback as a string inside a variable then call this variable with parentheses.

Let’s see an example for that.

<?php

$func = "codedtag";

function codedtag() {
  echo "Welcome to CodedTag.com Tutorials.";
}

$func(); // Welcome to CodedTag.com Tutorials.

How the above example works.

Firstly, I assigned a variable $func with a string value which contains the name of the function then build the function of codedtag. And then called the variable with parentheses like that: $func().

So the callback of this function would be $func() works as similar to codedtag(). But what about if the $func has only a string with undefined function?

It will show you an error, Let’s see an example.

<?php

$func = "calling_func";
$func(); 

This will show you an error like the following, because the variable has parentheses which means the variable has a callback name as a string calling_func and this is an undefined function.

Fatal error: Uncaught Error: Call to undefined function calling_func() in /tmp/index.php:4 Stack trace: #0 {main} thrown in /tmp/index.php on line ..  

Using a Method of Class as a Variable

Once you take an instance from the class you can use the methods inside as variables with parentheses, see the below example.

<?php
 
class team {
   public function group() {
      echo "Our team is ready to start the challenge";
   }
}

$obj = new team();
$group = "group";
$obj->$group();

So, in this example, the $group variable contains the method name which will work once invoked with parentheses.

If the class has a static property like the below.

<?php
 
class team {
   public static function group() {
      echo "Our team is ready to start the challenge";
   }
}

$obj = new team();
$group = "group";
$obj::$group();

It would be called with a scope resolution operator $obj::$group().

Also there is another way works for PHP 5.4 and above named with a complex callable.

<?php
 
$obj = new team();
$group = array($obj, "group");
$group();

If the class has a static function it can work as the below.

<?php
class team{
  public static function group() {
    echo "Static Method.";
  }
}
$group = array( "team", "group");
$group();

So, in these two examples, the array works as function, it implemented the class object with the function as callable once it finds the main variable has parentheses.

Assign a Function Callback to Variable with Arguments

You can do the same way and passing the arguments into the parentheses of variable.

<?php

$array = array( "item 1", "item 2", "item 3" );
$func = "count";
echo $func($array); // 3

So, the $func variable contains only a string with a predefined name in PHP which name “count” and once you invoke the $func variable with parentheses. PHP will look for the “count()” function, which is requiring a countable element as an argument.

But there are some PHP predefined callbacks will not work with the variable like the language constructs such as: require, empty, include, isset, print, unset, and echo.

Let’s see an example for that

<?php

$value = "Not Empty";
$is_empty = "empty";
var_dump($is_empty());

This example will show you an error like the below.

Fatal error: Uncaught Error: Call to undefined function empty()

So you can use the same way with all redefined PHP functions such as strlen, strtoupper, count, etc. But it will not work for the language constructs as I mentioned in the previous part

Wrapping Up

To assign a PHP function to the variable you need to store the function name inside the variable as a string value. Then invoke this variable with parentheses.