• Language Reference
  • References Explained

Passing by Reference

Note : There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference.

References returned from functions, i.e.: <?php function foo (& $var ) { $var ++; } function & bar () { $a = 5 ; return $a ; } foo ( bar ()); ?> See more about returning by reference .

No other expressions should be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid: <?php function foo (& $var ) { $var ++; } function bar () // Note the missing & { $a = 5 ; return $a ; } foo ( bar ()); // Produces a notice foo ( $a = 5 ); // Expression, not variable foo ( 5 ); // Produces fatal error class Foobar { } foo (new Foobar ()) // Produces a notice as of PHP 7.0.7 // Notice: Only variables should be passed by reference ?>

User Contributed Notes 17 notes

To Top

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

PHP - Can you assign a member function to a variable? [duplicate]

In PHP5, variables can be evaluated as functions 1 such as:

Is there any syntax for assigning object member functions to a variable such as:

PressingOnAlways's user avatar

2 Answers 2

There is a way, but for php 5.4 and above...

Hm.. actually it works for static too, just use the reference by name..

And for nonstatic this notation works as well. It creates a temporary instance of the class. So, this is what you need, only you need php 5.4 and above )

Cheery's user avatar

The PHP 5.4 solution above is good. If you need PHP 5.3, I don't think you can do much better than the anonymous function approach, but you could wrap that into a function that acts very similar to the PHP 5.4 method:

This should work for any number of arguments to any (publicly visible) method.

Austin's user avatar

Not the answer you're looking for? Browse other questions tagged php or ask your own question .

Hot Network Questions

php assign variable to function

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

PHP: Assign a function to a variable.

This is a short guide on how to assign an anonymous function to a variable in PHP and then call it. Note that these kind of functions are sometimes referred to as lambda functions .

Let’s start off with a simple example with no parameters:

In the code above, we created a very basic anonymous PHP function and assigned it to a variable called $myAnonymousFunction . As a result, we were then able to call the function in question by referencing $myAnonymousFunction .

Anonymous function with parameters.

Now, let’s create an anonymous PHP function that takes in parameters:

As you can see, this example doesn’t differ too much from our first one. The only difference is that it takes in a parameter called $name and prints it out.

Passing one anonymous function into another.

You can also pass one anonymous function in as a parameter to another anonymous function:

In the code above, we created two functions and assigned them to PHP variables. The first function prints out a string, whereas the second one takes in a function as a parameter before calling it. Furthermore, we passed the first function into the second function.

As a result, the script above will print the words “Hi everybody” to the browser.

Crazy, right?

Oh, and by the way, I am not sorry for putting Dr. Nick’s voice in your head.

w3docs logo

How to Pass Variables by Reference in PHP

Pass by reference, php objects, defining variables in php, related resources.

PHP variables , but not objects, by default, are passed by value. Once PHP variables are passed by value, the variable scope, defined at the function level is linked within the scope of the function. Modification of either of the variables has no impact.

Let’s see an example:

Watch a video course   Learn object oriented PHP

For passing variables by reference, it is necessary to add the ampersand ( & ) symbol before the argument of the variable.

An example of such a function will look as follows: function( &$x ).

The scope of the global and function variables becomes global. The reason is that they are defined by the same reference. Hence, after changing the global variable, the variable that is inside the function also gets changed.

Here is a clear example:

As a rule, PHP variables start with the $ sign followed by the variable name.

While assigning a text value to a variable, putting quotes around the value is necessary.

In contrast to other programming languages, PHP doesn’t have a command to declare a variable. It is generated at the moment a value is first assigned to it.

In PHP, variables are containers to store data.

The PHP variables may have both short( for instance, x and y) and more descriptive names (fruitname, age, height, and so on).

PHP Tutorial

Php advanced, mysql database, php examples, php reference, php variables scope.

In PHP, variables can be declared anywhere in the script.

The scope of a variable is the part of the script where the variable can be referenced/used.

PHP has three different variable scopes:

Global and Local Scope

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:

Variable with global scope:

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:

Variable with local scope:

You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.

Advertisement

PHP The global Keyword

The global keyword is used to access a global variable from within a function.

To do this, use the global keyword before the variables (inside the function):

PHP also stores all global variables in an array called $GLOBALS[ index ] . The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.

The example above can be rewritten like this:

PHP The static Keyword

Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.

To do this, use the static keyword when you first declare the variable:

Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.

Note: The variable is still local to the function.

PHP Exercises

Test yourself with exercises.

Create a variable named txt and assign the value "Hello" .

Get started with your own server with Dynamic Spaces

COLOR PICKER

colorpicker

Get your certification today!

php assign variable to function

Get certified by completing a course today!

Subscribe

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Your Suggestion:

Thank you for helping us.

Your message has been sent to W3Schools.

Top Tutorials

Top references, top examples, web certificates, get certified.

Data Structure

PHP Variable functions

Introduction.

If name of a variable has parentheses (with or without parameters in it) in front of it, PHP parser tries to find a function whose name corresponds to value of the variable and executes it. Such a function is called variable function. This feature is useful in implementing callbacks, function tables etc.

Variable functions can not be built eith language constructs such as include, require, echo etc. One can find a workaround though, using function wrappers.

Variable function example

In following example, value of a variable matches with function of name. The function is thus called by putting parentheses in front of variable

 Live Demo

This will produce following result. −

Here is another example of variable function with arguments

In following example, name of function to called is input by user

Variable method example

Concept of variable function can be extended to method in a class

A static method can be also called by variable method technique

This will now throw exception as follows −

Malhar Lathkar

0 Followers

Tutorials Point

IMAGES

  1. Use PHP variable as Javascript function parameters

    php assign variable to function

  2. How To Assign Function To Variable In Php?

    php assign variable to function

  3. Understand global variables scope in python

    php assign variable to function

  4. PHP Variable Usage inside of Function

    php assign variable to function

  5. PHP Variable Functions

    php assign variable to function

  6. What is PHP Variable and How to use?

    php assign variable to function

VIDEO

  1. Introduction to PHP Working with Variables

  2. #032 [PHP-COURSE] Variables & Selectors

  3. php://input

  4. 35- PHP Function Variable

  5. PHP Lecture 3: HOW TO DECLARE A VARIABLE IN PHP

  6. PHP Session, Variable and If Statement

COMMENTS

  1. PHP: Function arguments - Manual

    PHP supports passing arguments by value (the default), passing by reference, and default argument values. Variable-length argument lists and Named Arguments are also supported. Example #1 Passing arrays to functions <?php function takes_array($input) { echo "$input[0] + $input[1] = ", $input[0]+$input[1]; } ?>

  2. PHP: Passing by Reference - Manual

    You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows: <?php function foo(&$var) { $var++; } $a=5; foo($a); // $a is 6 here ?> Note: There is no reference sign on a function call - only on function definitions.

  3. PHP - Can you assign a member function to a variable?

    In PHP5, variables can be evaluated as functions 1 such as: function myFunc () { echo "whatever"; } $callableFunction = 'myFunc'; $callableFunction (); // executes myFunc () Is there any syntax for assigning object member functions to a variable such as:

  4. PHP: Assign a function to a variable. - This Interests Me

    PHP: Assign a function to a variable. This is a short guide on how to assign an anonymous function to a variable in PHP and then call it. Note that these kind of functions are sometimes referred to as lambda functions. Let’s start off with a simple example with no parameters:

  5. PHP Variables - W3Schools Online Web Tutorials

    PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error. In PHP 7, type declarations were added.

  6. How to Pass Variables by Reference in PHP - W3docs

    As a rule, PHP variables start with the $ sign followed by the variable name. While assigning a text value to a variable, putting quotes around the value is necessary. In contrast to other programming languages, PHP doesn’t have a command to declare a variable. It is generated at the moment a value is first assigned to it.

  7. PHP Variables Scope - W3Schools

    In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used. PHP has three different variable scopes: local. global.

  8. PHP Variable functions - tutorialspoint.com

    PHP Server Side Programming Programming Introduction If name of a variable has parentheses (with or without parameters in it) in front of it, PHP parser tries to find a function whose name corresponds to value of the variable and executes it. Such a function is called variable function.