note 98811 modified in language.variables.scope by danbrown

From: Date: Fri, 09 Jul 2010 12:46:14 +0000
Subject: note 98811 modified in language.variables.scope by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170816@lists.php.net to get a copy of this message
Please note for using global variable in child functions: This won't work correctly... <?php function foo(){ $f_a = 'a'; function bar(){ global $f_a; echo '"f_a" in BAR is: ' . $f_a . '<br />'; // doesn't work, var is empty! } bar(); echo '"f_a" in FOO is: ' . $f_a . '<br />'; } ?> This will... <?php function foo(){ global $f_a; // <- Notice to this $f_a = 'a'; function bar(){ global $f_a; echo '"f_a" in BAR is: ' . $f_a . '<br />'; // work!, var is 'a' } bar(); echo '"f_a" in FOO is: ' . $f_a . '<br />'; } ?> --was-- Please note for using global variable in child functions: This won't work correctly... <?php function foo(){ $f_a = 'a'; function bar(){ global $f_a; echo '"f_a" in BAR is: ' . $f_a . '<br />'; // doesn't work, var is empty! } bar(); echo '"f_a" in FOO is: ' . $f_a . '<br />'; } ?> This will... <?php function foo(){ global $f_a; // <- Notice to this $f_a = 'a'; function bar(){ global $f_a; echo '"f_a" in BAR is: ' . $f_a . '<br />'; // work!, var is 'a' } bar(); echo '"f_a" in FOO is: ' . $f_a . '<br />'; } http://php.net/manual/en/language.variables.scope.php

« previous php.notes (#170816) next »