How To Make Array Variable Global In PHP Function
Posted On Sat Feb 23, 2008 By hostman In PHP Forums And Topics Discussions About Hyper Text Programming Language Forums
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;
that wont work, so what you need to do is call the global array in your function, like this:
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];
?>
$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];
?>
$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];
?>
ALex Sun Jan 22, 2012
Don't ever go with go daddy!
RollYourOwnEd Thu Sep 08, 2011
Nice one guy, thanks!
Greg Sat Aug 20, 2011
- at - Julie; I have the same problem. A function that calls a global array will work on my localhost, but when live, the function does not work. The array is not recognized as global any more. Also, variables set to 0 (zero, not "NULL") behave properly on my localhost, but are imported to the function that calls them as global variables as "NULL" values when I use a live site (GODADDY). Anyone have a solution for this?
Nodehead Sat Aug 13, 2011
Use your custom keys?
function add(){
global $array_string;
echo $array_string['first'];
}
function add(){
global $array_string;
echo $array_string['first'];
}
Julie Wed Jul 27, 2011
So is there a version of php where an array can't be declared global in a function? My array is blank when I get to the function. Declaring it exactly as
$array_string = array(
'first' => 'example',
'second' => 'example');
function add() {
global $array_string;
echo $array_string[0];
}
comes up empty. any ideas?
$array_string = array(
'first' => 'example',
'second' => 'example');
function add() {
global $array_string;
echo $array_string[0];
}
comes up empty. any ideas?
Niixie Sat Jul 23, 2011
It works very well, thank you very much!
Han Tue May 31, 2011
Doesn't work when you try to access the array in a class constructor.
luben Wed May 11, 2011
Legend thanks!
Ark Tue Jan 04, 2011
oh man... spent the last hour looking for this... Thx a 1000 times!!
shaffy Mon Aug 02, 2010
its really useful, and help me Thanks you very much.
ocha Sun Jul 04, 2010
that helps :)
i've spent some days finding out why my array didnt work out.
thank u very very much..
regards
Ocha
i've spent some days finding out why my array didnt work out.
thank u very very much..
regards
Ocha
adsadsads Mon Jun 21, 2010
that code isnt even close to working
adsadsadsadsads
adsadsadsadsads
Ian Tue Sep 15, 2009
thank you!!! cheers
yo Sun Oct 26, 2008
cool comment dude , took me out of doubts , cheers.
Related Content
Information
Forums »
PHP Forums And Topics Discussions About Hyper Text Programming Language »
How To Make Array Variable Global In PHP Function
PHP Forums And Topics Discussions About Hyper Text Programming Language »
How To Make Array Variable Global In PHP Function
Title: How To Make Array Variable Global In PHP Function
Description: How To Make Array Variable Global In PHP Function
Tags: how ,to ,make ,array ,variable ,global ,in ,php ,function
Info: This Post Has Been Viewed 0 Times Since
Date: Sat Feb 23, 2008
Author hostman Received 14 Replies #1098
Date: Sat Feb 23, 2008
Author hostman Received 14 Replies #1098
Share
URL: 

Embed: 

To embed this topic, just copy the code from the "Embed" box. Once you've copied the code, just paste it into your website or blog to embed it
BBCODE:: 

BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums
wallpaperama | Wallpapers | Forums | Terms Of Service
copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5
copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5