Edit report at https://bugs.php.net/bug.php?id=61126&edit=1

 ID:                 61126
 User updated by:    cornelius dot howl at gmail dot com
 Reported by:        cornelius dot howl at gmail dot com
 Summary:            associative array syntax
 Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   Any
 PHP Version:        5.4.0RC7
 Block user comment: N
 Private report:     N

 New Comment:

Making new function to handle undefined key makes code slow. function call is 
really slow in PHP, and wrapping with a function makes code ugly.

I mean, an undefined hash should throw an error or notice (the behavior is 
right).

but when using an undefined key from a defined hash, PHP should not throw an 
error.


Previous Comments:
------------------------------------------------------------------------
[2012-02-18 11:45:03] ni...@php.net

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You access an undefined index so you obviously get a notice, nothing will 
change about that.

If you want, you can define a function like

    function getWithDefault(&$var, $default = null) {
        return isset($var) ? $var : $default;
    }
    $abc = getWithDefault($def['abc'], 'ghi');

------------------------------------------------------------------------
[2012-02-17 17:54:01] cornelius dot howl at gmail dot com

Description:
------------
When using undefined $args['method'] this should not throw an error, instead, 
it 
should return undef or null.

In Perl or Ruby, Python, we usually write:

   $value = $hash{key} || 'default value';

But in PHP, we have to write such code, that's such inefficient:

   $value = isset($hash['key']) ? $hash['key'] : 'default';

Yes, we can use '@', but the '@' makes PHP runs more slower.

It will be better if we can have the following syntax:

   $value = $hash['key'] || 'default value';

so that if you have multiple hashes:

   $value = $hash['key'] || $hash2['key'] || $hash3['key'] || 'last value';

then we don't have to write:

   $value = isset($hash['key']) ? $hash['key'] : isset($hash2['key']) ? 
$hash2['key'] : $hash2['key'] ........ # whatever



Thanks



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=61126&edit=1

Reply via email to