note 102790 added to function.compact

From: Date: Mon, 07 Mar 2011 04:12:53 +0000
Subject: note 102790 added to function.compact
Groups: php.notes 
Request: Send a blank email to php-notes+get-177604@lists.php.net to get a copy of this message
There certainly is a time and place for compact(), but my official stance on it is that it should never be nested within another function or class method. I've written a simple example below to show how it does not evaluate as an array until AFTER the outer method has been started. <?php /* * RUN THIS SCRIPT... * Outputs "Thomas the Cat" * * THEN UNCOMMENT THE LAST LINE... * PHP Parse error: syntax error, unexpected '[' */ $tom = "Thomas the Cat"; $jerry = "Jerry the Mouse"; $cat_and_mouse = compact('tom', 'jerry'); print_r($cat_and_mouse['tom']); // UNCOMMENT THE NEXT LINE TO SEE THE 'compact' TEST // print_r(compact('tom', 'jerry')['tom']); ?> ---- Server IP: 192.41.42.26 Probable Submitter: 75.21.73.180 ---- Manual Page -- http://www.php.net/manual/en/function.compact.php Edit -- https://master.php.net/note/edit/102790 Del: integrated -- https://master.php.net/note/delete/102790/integrated Del: useless -- https://master.php.net/note/delete/102790/useless Del: bad code -- https://master.php.net/note/delete/102790/bad+code Del: spam -- https://master.php.net/note/delete/102790/spam Del: non-english -- https://master.php.net/note/delete/102790/non-english Del: in docs -- https://master.php.net/note/delete/102790/in+docs Del: other reasons-- https://master.php.net/note/delete/102790 Reject -- https://master.php.net/note/reject/102790 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177604) next »