php - to Global Variables inside functions -
i have problem global variables inside functions
<?php function main(){ $var = "my variable"; function sub() { global $var; echo $var; // show "my variable" } sub(); echo $var; // show "my variable" } main(); sub(); // not show , sub() cant use outside main() function ?> - i want global
$varinside sub functions sub()not work outsidemain()function
i tied use global show nothing ... ?
not sure if understand want, $var not global. local variable inside main()
a variable global, if declare outside of function or class.
<?php $var = "my variable"; // made $var global function main(){ //removed $var here function sub() { global $var; echo $var; // show "my variable" } sub(); echo $var; // throw notice: undefined variable: var } main(); sub(); // show "my variable" ?> why declare method inside method call there?
maybe want...
<?php //$var = "my variable"; function main(){ $var = "my variable"; $sub = function($var) { echo "sub: ".$var; // show "sub: variable" }; $sub($var); echo "main: ".$var; // show "main: variable" } main(); // sub(); // not work // $sub(); // not work ?>
Comments
Post a Comment