PHP Coding Tips

Tuesday, August 7, 2012

How to access global variable in php ?

If you are in a function on class method scope, you can gain access to a global variable by using a global keyword.$x = 5; function myFunc() { global $x; echo $x; }Remeber that using globals usually isn't a good idea. Better idea would be divide your globals by categories/modules and store it in static variables of some classes.

No comments:

Post a Comment