note 102790 added to function.compact

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.