How To Make Array Variable Global In PHP Function

RegisterLogin
How To Make Array Variable Global In PHP Function
Post Description: How To Make Array Variable Global In PHP Function
Tags: How, To, Make, Array, Variable, Global, In, PHP, Function
This Post Was Posted On Feb 29, 2008 By web hosting #1304
today i had this problem, i had an array and i wanted to use it in a function, but when i tried to use it it wouldn't work. so if you are also having this issue, hopefully you can learn from my mistakes.

lets say for example i have this php code;



<?php
$array_string = array();
$array_string[0] = "zero";
$array_string[1] = "one";
$array_string[2] = "two";
$array_string[3] = "three";
function ($string){
echo $array_string[3];
?>


that wont work, so what you need to do is call the global array in your function, like this:
<?php
$array_string = array();
$array_string[0] = "zero";
$array_string[1] = "one";
$array_string[2] = "two";
$array_string[3] = "three";
function ($string){
global $array_string;
echo $array_string[3];
?>
©2011 Webune Forums - Tue Dec 20, 2011 4:38 am
Powered by: Webune Forums V3