Background
In every other programming language I use on a regular basis, it is simple to operate on the return value of a function without declaring a new variable to hold the function result.
In PHP, however, this does not appear to be so simple:
<?php function foobar(){ return preg_split('/\s+/', 'zero one two three four five'); } // can php say "zero"? /// print( foobar()[0] ); /// <-- nope /// print( &foobar()[0] ); /// <-- nope /// print( &foobar()->[0] ); /// <-- nope /// print( "${foobar()}[0]" ); /// <-- nope ?>
<?php function zoobar(){ // NOTE: casting (object) Array() has other problems in PHP // see e.g., http://stackoverflow.com/questions/1869812 $vout = (object) Array('0'=>'zero','fname'=>'homer','lname'=>'simpson',); return $vout; } // can php say "zero"? // print zoobar()->0; // <- nope (parse error) // print zoobar()->{0}; // <- nope // print zoobar()->{'0'}; // <- nope // $vtemp = zoobar(); // does using a variable help? // print $vtemp->{0}; // <- nope
=> is the separator for associative arrays. In the context of that foreach loop, it assigns the key of the array to $user and the value to $pass .
Array Dereferencing is really very good feature added in PHP 5.4. With this you can directly access an array object directly of a method a functions. Now we can say that no more temporary variables in php now.
PHP can not access array results from a function. Some people call this an issue, some just accept this as how the language is designed. So PHP makes you create unessential variables just to extract the data you need.
So you need to do.
$var = foobar(); print($var[0]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With