Now if only when a function returns an array we needn't a variable... function boo(){ return array(1,2,3) } echo boo()[1];
I don't see how this will ever be useful. The state "echo boo()[1];" assumes you're only ever going to need that one value from the array. The rest of the array is lost. What if the next line in the code needs index 3 from the array? Now you have to run the function again. I also don't agree that PHP needs constructors to return $this for the same reason. You need to assign the object to a variable so you can reuse i…
Short array syntax finally in PHP 5.4
51–60 of 80 posts
Re: Short array syntax finally in PHP 5.4
#52Earlier quoted context omitted.
It's a pretty common feature of a lot of languages (Python, Perl, etc) so it's really not that irregular.
It looks more obviously "language feature" in Perl, Python, and JavaScript, though: my ($foo, $bar) = f; (foo, bar) = f() var [foo, bar] = f();
list($foo, $bar) = f();
Given that PHP uses the array() keyword for constructing arrays, it is perfectly symmetrical. My IDE highlights the list() keyword just the same as all the other keywords.Re: Short array syntax finally in PHP 5.4
#53Still strikes me as a useless bit of syntactic sugar. Is it really that much work to write array('foo' => 'bar')? As far as I can tell, writing ['foo' => 'bar'] isn't that much shorter in the first place. sigh Can the PHP devs focus on the real problems now? cough Unicode anyone? Ridiculously inconsistent naming of string and array functions? The woefully incomplete and useless STL? The lack of OO in arrays and strin…
An actual patch has been submitted for this before, and rejected by the PHP high muck-a-mucks, even though the community desperately wanted it, and the work was already done.
The fact that they finally gave in and included it means not only do we get a nice bit of sugar to sweeten up our programs, but also that maybe the community CAN steer the devs towards issues they care about.
Time will tell.
Re: Short array syntax finally in PHP 5.4
#54Even better would be bool/float/int/string/func ref type hinting...
Re: Short array syntax finally in PHP 5.4
#55Re: Short array syntax finally in PHP 5.4
#56 $foo = $_POST["foo"];
Undefined array key! Oh, no! Tragedy! A crime has been committed! Are you kidding? Just set $foo to undefined and let me deal with the consequences, please?Re: Short array syntax finally in PHP 5.4
#57Earlier quoted context omitted.
They're trying to be nicer to the humans. (Though IMO, any human who wanted to be kind to himself would just not use PHP to begin wtih.)
Right now those humans would not choose to start a project in php, but a few years ago there were not too much alternatives. It's not for nothing that the world's second biggest website is written in php.
Re: Short array syntax finally in PHP 5.4
#58Adding fancy syntax does not magically transform that crapware to something almost perfect like python3 or ruby (in its way) where these syntactic forms are among foundations of the language and are supported through all basic idioms. ^_^
Re: Short array syntax finally in PHP 5.4
#59My complaint about arrays in PHP is warnings for reading a array member that may not exist, such as when reading a form input that might not be there. $foo = $_POST["foo"]; Undefined array key! Oh, no! Tragedy! A crime has been committed! Are you kidding? Just set $foo to undefined and let me deal with the consequences, please?
I fundamentally disagree with you though. For me, notices are very useful for identifying typos and other coding mistakes that can lead to incorrect behavior.
Consider the following lines of code:
$user_info = array(
'id' => 1,
'username' => 'nbpoole',
'is_guess' => false
);
if (!$user_info['is_guest'])
{
echo 'Member stuff';
}
When I run that script with error_reporting set to E_ALL, I get an "undefined index" notice pointing me to the line where I do the comparison: at that point, I can track down where in my code I misspelled the word guest. Without the notice, I might not immediately realize that the 'is_guest' index was mis-spelled somewhere.Re: Short array syntax finally in PHP 5.4
#60My complaint about arrays in PHP is warnings for reading a array member that may not exist, such as when reading a form input that might not be there. $foo = $_POST["foo"]; Undefined array key! Oh, no! Tragedy! A crime has been committed! Are you kidding? Just set $foo to undefined and let me deal with the consequences, please?