Jump to content

reach of variables


voodoochicken

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...