News for hackers. Stuff that matters.
Re:Short array syntax finally in PHP 5.4 by snorkel on Sunday July 24, 2011 @09:51PM (#2800972)
My 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?
Re:Short array syntax finally in PHP 5.4 by nbpoole on Monday July 25, 2011 @12:00AM (#2801274)
They're notices, not warnings. They can be disabled via a simple call to the error_reporting function.

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 by code_duck on Monday July 25, 2011 @12:40AM (#2801375)
You really wouldn't like Python or even stricter languages such as Scala or java, then. In Python, an attempt to access a nonexistent dictionary key will throw an exception which halts execution if not caught. (similar to calling a nonexistent method or function in PHP, but since PHP doesn't have a well-thought out exception system/std. lib as Python does, you can't catch anything in that situation in PHP).
Re:Short array syntax finally in PHP 5.4 by cheald on Monday July 25, 2011 @06:07PM (#2804933)
In Python, if you're expecting a potentially missing key, you can just use dict.get('key') rather than dict['key']. The equivalent in PHP would be something like:

  $val = array_key_exists('key', $dict) ? $dict['key'] : NULL;
Being able to say "I expect a potentially nil value (and am okay with that)" via get() rather than array index is both semantically useful, and far more beautiful than the PHP equivalent.
Re:Short array syntax finally in PHP 5.4 by code_duck on Tuesday July 26, 2011 @12:13PM (#2808006)
Sure, or you can catch a KeyError and do something else entirely. I'm aware of all this, just pointing out some differences to the person to whom I replied.

Python's dict.get is a bit like the getOrElse found in Scala. It's quite handy and can be seen as advanced conceptually.

I actually added some code like that to a recent post about PHP here: http://news.ycombinator.com/item?id=2771304

Re:Short array syntax finally in PHP 5.4 by Luyt on Monday July 25, 2011 @07:24PM (#2805185)
The more so because get() can also take a default value to return if the key is not present in the dict ;-)
Re:Short array syntax finally in PHP 5.4 by tintin on Monday July 25, 2011 @05:00AM (#2801871)
I think those warnings are very helpful. They help to develop better and more secure code.

Ever tried to assign a non-existing variable in another language? I think you will get a compile error.

This isn't too hard:

  $foo = isset($_POST["foo"]) ? $_POST["foo"] : null;
Re:Short array syntax finally in PHP 5.4 by kylewpppd on Monday July 25, 2011 @12:05AM (#2801289)
This is exactly what PHP does. Yes, it issues you a notice, not a warning, not an exception. And it does not break execution. Just set your error_reporting variable to a lower level, and you won't get the Notice at all.
Re:Short array syntax finally in PHP 5.4 by ubuntuftw on Monday July 25, 2011 @09:31AM (#2802517)
I think they're helpful too! Just use isset() to check whether variables exist, and you shouldn't get any errors unless you make a mistake (in which case, wouldn't it be nice to know?).