php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61126 associative array syntax
Submitted: 2012-02-17 17:54 UTC Modified: 2012-02-22 07:10 UTC
From: cornelius dot howl at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.0RC7 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: cornelius dot howl at gmail dot com
New email:
PHP Version: OS:

 

 [2012-02-17 17:54 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-18 11:45 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2012-02-18 11:45 UTC] nikic@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-22 06:44 UTC] cornelius dot howl at gmail dot com
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.
 [2012-02-22 07:10 UTC] rasmus@php.net
People are just as apt to make mistakes in key names as they are in hash names.

$_SERVER['REQUEST_URL'] when you meant to check $_SERVER['REQUEST_URI'], for 
example. So I don't agree there is no value in a notice on trying to access an 
array element that hasn't been defined.

I don't disagree with the notion of having a clean operator that you can use 
explicitly on undefined array elements. We have a number of functions that are 
specifically aimed at these. This is a valid feature request.
 [2012-02-22 07:10 UTC] rasmus@php.net
-Type: Bug +Type: Feature/Change Request
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 20:01:32 2025 UTC