voodoochicken Posted February 14, 2008 Share Posted February 14, 2008 hi, i wrote the following script (thats all the script, copy-paste)<?php$algo="una cadena asignada a algo";function prueba() { if (isset($algo)) { echo "definido "; } else { echo "no definido "; } echo $algo;}prueba();echo "fin";?>the output is 'no definido fin'it means the 'algo' var is not found inside the function. can anybody explain the reach of variables? i mean, if i can not find in a function the var i defined in the same script, not aslocal inside another function, how does the reach of variables work?tnx Link to comment Share on other sites More sharing options...
AElliott Posted February 14, 2008 Share Posted February 14, 2008 This is called scope, this will probably help: http://uk2.php.net/variables.scope Link to comment Share on other sites More sharing options...
justsomeguy Posted February 14, 2008 Share Posted February 14, 2008 That is called variable scope. The variable $algo is defined as a global variable. If you want to access a global variable inside a function you need to tell the function you want to use the global version. You can either use the global keyword or the $GLOBALS array. $algo="una cadena asignada a algo";function prueba() { global $algo; if (isset($algo)) { echo "definido "; } else { echo "no definido "; } echo $GLOBALS['algo'];} Link to comment Share on other sites More sharing options...
voodoochicken Posted February 14, 2008 Author Share Posted February 14, 2008 function prueba() { global $algo;tnx both of you, it worked! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.