How to Access Outer Local Variable in PHP

Is it possible to access outer local variable in PHP?

You could probably use a Closure, to do just that...


Edit : took some time to remember the syntax, but here's what it would look like :

function foo()
{
$l = "xyz";
$bar = function () use ($l)
{
var_dump($l);
};
$bar();
}
foo();

And, running the script, you'd get :

$ php temp.php
string(3) "xyz"


A couple of note :

  • You must put a ; after the function's declaration !
  • You could use the variable by reference, with a & before it's name : use (& $l)

For more informations, as a reference, you can take a look at this page in the manual : Anonymous functions

Access variable from function inside of nested function

You have two options:

  1. Give $content as a parameter

    function foo() {
    $content = 'foobar';

    function bar($content) {
    echo $content . '1'; // 'foobar1'
    }

    bar();
    }

    foo();
  2. Use closures (PHP Manual) (PHP 5.3.0+)

    Please note that the function declaration is a bit different compared to the 'conventional way' of declaring functions.

    function foo() {
    $content = 'foobar';

    $bar = function() use ($content) {
    echo $content . '1';
    }; // <-- As it's an assignment, end with a ';'

    $bar();
    }

    foo(); // 'foobar1'

Access local variable in function from outside function (PHP)

Its not possible. If $variable is a global variable you could have access it by global keyword. But this is in a function. So you can not access it.

It can be achieved by setting a global variable by$GLOBALS array though. But again, you are utilizing the global context.

function outside() {
$GLOBALS['variable'] = 'some value';
inside();
}

function inside() {
global $variable;
echo $variable;
}

Accessing outer variable in PHP 7 anonymous class

another solution could be

$outer = 'something';

$instance = new class($outer) {

private $outer;

public function __construct($outer) {
$this->outer = $outer
}

public function testing() {
var_dump($this->outer);
}
};

Outer Variable Access in PHP Class

Use a global (not recommended), a constant or a singleton configuration class.

Simply including

$tablePages = 'orweb_pages';

will give your variable local scope so it won't be visible inside other classes. If you use a constant:

define('TABLE_PAGES', 'orweb_pages');

TABLE_PAGES will be available for read access throughout the application regardless of scope.

The advantage of a constant over a global variable is that you dont have to worry about it being overridden in other areas of the application.

Giving my function access to outside variable

By default, when you are inside a function, you do not have access to the outer variables.


If you want your function to have access to an outer variable, you have to declare it as global, inside the function :

function someFuntion(){
global $myArr;
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;
}

For more informations, see Variable scope.

But note that using global variables is not a good practice : with this, your function is not independant anymore.


A better idea would be to make your function return the result :

function someFuntion(){
$myArr = array(); // At first, you have an empty array
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal; // Put that $myVal into the array
return $myArr;
}

And call the function like this :

$result = someFunction();


Your function could also take parameters, and even work on a parameter passed by reference :

function someFuntion(array & $myArr){
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal; // Put that $myVal into the array
}

Then, call the function like this :

$myArr = array( ... );
someFunction($myArr); // The function will receive $myArr, and modify it

With this :

  • Your function received the external array as a parameter
  • And can modify it, as it's passed by reference.
  • And it's better practice than using a global variable : your function is a unit, independant of any external code.


For more informations about that, you should read the Functions section of the PHP manual, and,, especially, the following sub-sections :

  • Functions arguments
  • Returning values

PHP function use variable from outside

Add second parameter

You need to pass additional parameter to your function:

function parts($site_url, $part) { 
$structure = 'http://' . $site_url . 'content/';
echo $structure . $part . '.php';
}

In case of closures

If you'd rather use closures then you can import variable to the current scope (the use keyword):

$parts = function($part) use ($site_url) { 
$structure = 'http://' . $site_url . 'content/';
echo $structure . $part . '.php';
};

global - a bad practice

This post is frequently read, so something needs to be clarified about global. Using it is considered a bad practice (refer to this and this).

For the completeness sake here is the solution using global:

function parts($part) { 
global $site_url;
$structure = 'http://' . $site_url . 'content/';
echo($structure . $part . '.php');
}

It works because you have to tell interpreter that you want to use a global variable, now it thinks it's a local variable (within your function).

Suggested reading:

  • Variable scope in PHP
  • Anonymous functions

How to access to variable in outside the map function in Laravel PHP

You need to make sure the anonymous function has access to the variable using the use keyword.

You would use it like this:

$collection = $collection->map(function ($item, $key) use ($x) {

return $item + $x;
});


Related Topics



Leave a reply



Submit