Re: array_push

From: Date: Tue, 27 Aug 2002 21:37:28 +0000
Subject: Re: array_push
Groups: php.general 
Request: Send a blank email to php-general+get-114287@lists.php.net to get a copy of this message
>why this array_push($x ? $a : $b, 'value'); produce error(Fatal error: >Only variables can be passed by reference) > ><?php > >$x = 1; >#$x = 0; > >$a = array(); >$b = array(); > >array_push($x ? $a : $b, 'value'); > >i must rewrote this in > >if ($x) > array_push($a, 'value'); >else > array_push($b, 'value'); My Theory: In order for array_push to be reasonably efficient, the array is passed by reference. If it were passed by value, PHP would have to make a COPY of the array, and the array could be VERY large... $x ? $a : $b has to turn into a "temporary" variable, since you don't really provide a storage place for it. That temporary variable can't be turned into a reference. At any rate, smushing the ? and the array_push all into one line is just being obtuse. Spell out what you're doing with an extra line of code and another variable: $victim = $x ? $a : $b; array_push($victim, 'value'); -- Like Music? http://l-i-e.com/artists.htm

« previous php.general (#114287) next »