Zend PHP 7 Certification – Functions – Variables

This post covers the Variables section of the Functions chapter when studying for the Zend PHP 7 Certification.

Variables declared within functions are only visible in that function. Similarly, variables declared outside of functions are visible everywhere outside of functions. In PHP, there is a global scope. Any variable declared outside of any function is within this global scope.

<?php
$foo = 'bar';

function myFunc() {
    $baz = 42;

    echo $foo;  // doesn't work
    echo $baz;  // works
}

echo $foo;  // works
echo $baz;  // doesn't work

The global keyword can be used to modify the scope of a variable $foo.

<?php
$foo = 'bar';

function baz() {
    global $foo;
    echo $foo; // Works
    $foo = 'baz';
}

Although the scope of a variable can be modified, it is not advised to do so as some functions keep modifying and requiring some global state, and ideally, you want functions to be stateless.

Variable functions work similarly to variable variables. If a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it.

<?php
function foo() {
    echo "In foo()
\n"; } $func = 'foo'; $func(); // This calls foo() and outputs: In foo() function foo($string) { echo $string; } $func = 'foo'; $func('hello'); // Calls foo() and outputs: hello

Note that you can also use the call_user_func() function to achieve the same result. This function takes two parameters: the function name as a string, and parameters to be passed to the callback.

<?php
function foo() {
    echo "In foo()
\n"; } $func = 'foo'; call_user_func($func); // Outputs: In foo()

And an example of using both parameters.

<?php
function increment(&$var) {
    $var++;
}

$a = 0;
call_user_func('increment', $a);
echo $a."\n"; // This outputs: 0

// You can use this instead
call_user_func_array('increment', array(&$a));
echo $a."\n"; // This outputs: 1

The first option will also throw a warning: Parameter 1 to increment() expected to be a reference, value given.

Object methods can also be called using variable functions.

<?php
class Foo
{
    function Variable() {
        $name = 'Bar';
        $this->$name(); // This calls the Bar() method
    }
    
    function Bar() {
        echo "This is Bar";
    }
}

$foo = new Foo();
$funcname = "Variable";
$foo->$funcname();  // This calls $foo->Variable() then calls $foo->Bar() and outputs: This is Bar

When calling static methods using variable functions, static methods take higher precedence than static properties. Consider the following:

<?php
class Foo
{
    static $variable = 'static property';
    static function Variable() {
        echo 'Method Variable called';
    }
}

echo Foo::$variable; // This prints 'static property'. It does need a $variable in this scope.
$variable = "Variable";
Foo::$variable();  // This calls $foo->Variable() reading $variable in this scope.

View the other sections:

Note: This article is based on PHP version 7.1.