Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access global variable inside function

This (simplified version of my code) doesn't work:

<?php     $sxml = new SimpleXMLElement('<somexml/>');      function foo(){         $child = $sxml->addChild('child');     }      foo(); ?> 

Why? I want to access $sxml because I want to log errors on it if foo() fails. foo() calls itself recursively to create a directory listing, so I fear passing the whole $sxml onto itself (as in foo($sxml)) could hurt performance.

Is there a way to access $sxml inside $foo without passing it as an argument? (PHP 5.2.x+)

EDIT: What if the code looks like this, actually?

<?php     bar(){         $sxml = new SimpleXMLElement('<somexml/>');         function foo(){             $child = $sxml->addChild('child');         }         foo();     }     bar(); ?> 
like image 888
Camilo Martin Avatar asked Mar 27 '11 13:03

Camilo Martin


People also ask

Can global variables be accessed inside functions?

Global variables can be used by everyone, both inside of functions and outside.

Can I use global variable in function JavaScript?

Variables declared Globally (outside any function) have Global Scope. Global variables can be accessed from anywhere in a JavaScript program.


1 Answers

You have to pass it to the function:

<?php     $sxml = new SimpleXMLElement('<somexml/>');      function foo($sxml){         $child = $sxml->addChild('child');     }      foo($sxml); ?> 

or declare it global:

<?php     $sxml = new SimpleXMLElement('<somexml/>');      function foo(){         global $sxml;         $child = $sxml->addChild('child');     }      foo(); ?> 

If the variable isn't global but is instead defined in an outer function, the first option (passing as an argument) works just the same:

<?php     function bar() {         $sxml = new SimpleXMLElement('<somexml/>');         function foo($sxml) {             $child = $sxml->addChild('child');         }         foo($sxml);     }     bar(); ?> 

Alternatively, create a closure by declaring the variable in a use clause.

<?php     function bar() {         $sxml = new SimpleXMLElement('<somexml/>');         function foo() use(&$xml) {             $child = $sxml->addChild('child');         }         foo();     }     bar(); ?> 
like image 109
Javi R Avatar answered Oct 02 '22 17:10

Javi R