note 100681 added to function.array-walk

From: Date: Sat, 30 Oct 2010 07:27:04 +0000
Subject: note 100681 added to function.array-walk
Groups: php.notes 
Request: Send a blank email to php-notes+get-173605@lists.php.net to get a copy of this message
Here's a handy function that illustrates using an array_walk that uses a create_function as the callback which in turn uses another array_walk with another create_function that outputs the global variables pretty-print style. Note that by using $g=array() as the first arg for array_walk it isn't necessary to declare the array beforehand. Other notes: ob_start, (print), and array_walk all return true on success which is how the if processing works. Using <?php !!$array; ?> returns true only if sizeof array is greater than 0, just a shortcut for <?php if(sizeof($array)>0);?> ... more in the php manual section on booleans. The output is meant to be displayed inline on a page so the htmlspecialchars is called on the output. The output is buffered by ob_start and is then output by echoing the return of ob_get_clean(), which returns the buffer and cleans the buffer. DEBUG would need to be a defined constant for this to execute, personally I like to <?php if($_SERVER['REMOTE_ADDR']=='MYSTATICIPADDRESS') askapache_global_debug(); //or if($_SERVER['REMOTE_ADDR']=='MYSTATICIPADDRESS') define('DEBUG',TRUE); ?> <?php function askapache_global_debug() { array_walk( $g=array('_GET','_POST','_COOKIE','_SESSION','_ENV','_SERVER'), create_function('$n','global $$n; if( !!$$n && ob_start() && (print "( $"."$n )\n") && array_walk($$n, create_function(\'&$v,$k\', \'echo "[$k] => $v\n";\')) ) echo "<pre>".htmlspecialchars(ob_get_clean())."</pre>";' ) ); } if(DEBUG) askapache_global_debug(); ?> ---- Server IP: 208.69.120.55 Probable Submitter: 24.123.215.58 ---- Manual Page -- http://www.php.net/manual/en/function.array-walk.php Edit -- https://master.php.net/note/edit/100681 Del: integrated -- https://master.php.net/note/delete/100681/integrated Del: useless -- https://master.php.net/note/delete/100681/useless Del: bad code -- https://master.php.net/note/delete/100681/bad+code Del: spam -- https://master.php.net/note/delete/100681/spam Del: non-english -- https://master.php.net/note/delete/100681/non-english Del: in docs -- https://master.php.net/note/delete/100681/in+docs Del: other reasons-- https://master.php.net/note/delete/100681 Reject -- https://master.php.net/note/reject/100681 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173605) next »