PHP - Setting a variable to a equal a function, without running it

variable function in php
php assign function to variable
php function
php global variable
php function use variable
difference between static and global variable in php
anonymous function in php
php class variables

Do you know if there is a way of doing the following in php without executing the function?

I would like $test to equal the function, not its result.

$test = helloWorld();

function helloWorld() {
    echo "HelloWorld";
}

I think the closest way to achieve this is to use a variable function:

function helloWorld() {
    echo "HelloWorld";
}

$test = 'helloWorld';
$test();

Using an anonymous function could also do it. Notice the ; after the function declaration.

$test = function () {
    echo "HelloWorld";
};

$test();

See variable functions and anonymous functions in the documentation.

Variable functions - Manual, Change language: Variable functions won't work with language constructs such as echo, print, unset(), isset(), empty(), include, require and the like. When calling static methods, the function call is stronger than the static property operator:. Further dereferencing a variable property that is an array has different semantics between PHP 5 and PHP 7. The PHP 7.0 migration guide includes further details on the types of expressions that have changed, and how to place curly braces to avoid ambiguity. Curly braces may also be used,

Answering my own question:

$test = function() {
    echo "HelloWorld";
};

// To call it:
$test();

I read all that functional stuff, but I sometimes despair about how little of it went in!

Anonymous functions - Manual, Inherited variable's value is from when the function // is defined, not Global variables exist in the global scope, which is the same no matter what function is executing. Without the '&', the anonymous function gets the value of $fib at the time the function is being created. The equivalent way to do that in PHP 5.3+ is PHP supports the concept of variable functions. This means that 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. Among other things, this can be used to implement callbacks, function tables, and so forth.

You can do the following

<?php

$test = "helloWorld";

function helloWorld () : void
{
    echo "Hello World!";
}

// Execute the code on demand using:
$this->{$test}();

Variable scope - Manual, So if you declare a variable as static inside a function, it's static for the whole class and without actually including it (to take advantage of my path/etc preprocessing, function, variables in the included file go into the scope of the calling function. check global variable outside function => 1 (equal to value of local variable  Notice that you need to use curly braces around the variable name in the echo statement so that PHP knows where the variable name is. If you use the statement without the curly braces, the output is as follows:

Say for example i have a page called functions.php now in functions im going to create the following

//functions.php page
class functions
{
    public function helloWorld() {
        echo "Hello World";
    }
}

No that i have a class and a function inside it i would like to call it on my index page so i will now have to do the following

//index.php page
$test = new functions(); //calls the class functions 

//here is the part you would like
//$test->helloWorld(); this calls the helloWorld function but does nothing.

//now you can store this into a variable
$helloWorld = $test->helloWorld();

Variable variables - Manual, That is, a variable name which can be set and used dynamically. variable variables cannot be used with PHP's Superglobal arrays within functions or object out of the array it was stored in without actually knowing its name. These are the scenarios that you may run into trying to reference superglobals dynamically. Comparison Operators. Comparison operators, as their name implies, allow you to compare two values. You may also be interested in viewing the type comparison tables, as they show examples of various type related comparisons.

Function arguments - Manual, A function may define C++-style default values for scalar arguments as follows: If a file without strict typing enabled makes a call to a function that was defined in calling functions to unpack an array or Traversable variable or literal into the  Caution. The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

How to Define and Call a Function in PHP, In this tutorial you will learn how to define and call a custom function in PHP to the same task by calling this function wherever you want within your script without work like placeholder variables within a function; they're replaced at run time parameter name, followed by an equals ( = ) sign, followed by a default value,  In PHP, a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value. After declaring a variable it can be reused throughout the code. The assignment operator (=) used to assign value to a variable.

PHP Functions, Besides the built-in PHP functions, it is possible to create your own functions. A function will not execute automatically when a page loads. PHP automatically associates a data type to the variable, depending on its value. following example we try to send both a number and a string to the function without using strict :  How JavaScript variable are retrieved from PHP script? And conversely, how to use the values of PHP variables in JavaScript? Form's data will be in $_POST or $_GET . Using the content of forms in PHP. Name of elements of a form are also PHP variables as soon as the PHP script is the action of the form. Example, the complete form:

Comments
  • Bud, to make life a little easier, make a class create the functions within it and simply call the functions within the class
  • You can do $test = function () { echo "HelloWorld"; }, this is called an anonymous function.
  • @Robbie if you don't want to execute the function in your own words. Why create the function in the first place ?
  • I don't want it executing at the moment it's defined, that's all.
  • Thanks AymDev. I'll end up using both of these methods I reckon, the first for the predefined Wordpress functions I'm having to use, and the second for my own functions.
  • @Robbie I answered for your needs but you maybe should just call WP functions normally and declare your custom functions in a more classic way. Happy coding !
  • After much messing about, I've concluded exactly that AymDev.
  • Thanks for the swift reply, which does exactly what I need, though I'm not OO, so don't need the $this->{};
  • @Robbie But ... this executes the function. How is this exactly what you need?
  • @Robbie have a look at my answer :)
  • kerbholz - It doesn't execute the function on its creation, only when I give it the $this->{$test}(); which is much later in the code, which is what I'm after.
  • Thanks JamesBond. I'm trying to get away from OO like I'm being told. The imperative/functional principle is the same though, so thanks for the help.