note 26015 added to language.variables.scope

From: Date: Tue, 15 Oct 2002 10:12:43 +0000
Subject: note 26015 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-38099@lists.php.net to get a copy of this message
Some people (including me) had a problem with defining a long GLOBAL variable list in functions (very error prone). Here is a possible solution. My program parses php file for functions, and compiles GLOBAL variable lists. Then you can just remove from the list those variables which need not be global. <?php //parser for GLOBAL variable list $pfile=file("myfile.php4"); for($i=0;$i<sizeof($pfile);$i++) { if(eregi("function",$pfile[$i])) { list($part1,$part2)=sscanf($pfile[$i],"%s %s"); echo "\n\n $part1 $part2:\nGLOBAL "; $varlist=array(); $level=0; $end=$i; do { $lpar=explode("{",$pfile[$end]); $level+=sizeof($lpar)-1; $lpar=explode("}",$pfile[$end]); $level-=sizeof($lpar)-1; $end++; } while(($end<sizeof($pfile))&&($level>0)); $pstr=""; for($j=$i;$j<=$end;$j++) $pstr.=$pfile[$j]; $lpar=explode("$",$pstr); for($j=1;$j<sizeof($lpar);$j++) { eregi('[a-zA-Z_][a-zA-Z0-9_]*',$lpar[$j],$cvar); $varlist[$cvar[0]]=1; } array_walk($varlist,'var_print'); } } function var_print ($item, $key) { echo "$key,"; } ?> -- http://www.php.net/manual/en/language.variables.scope.php http://master.php.net/manage/user-notes.php?action=edit+26015 http://master.php.net/manage/user-notes.php?action=delete+26015 http://master.php.net/manage/user-notes.php?action=reject+26015

« previous php.notes (#38099) next »