PHP冗余变量

I find myself creating redundant variables in PHP when querying the database.

In Javascript, C# and Java I'm able to directly use an array index operator after a method call, where as in PHP, I can't.

The following example illustrates my point:

// $result -> SELECT t.id
//            FROM table t
//            WHERE t.name = 'bla'
//            LIMIT 1    

$o = mysql_fetch_assoc($result);
$value = $o['valueIndex'];

And this would be invalid:

$value = mysql_fetch_assoc($result)['valueIndex'];

Why is the above invalid, did they do this by design? Or would the grammar get too complicated?

Little fiddle over here.

This will be implemented in PHP 5.4 as it stands currently.

In your specific case you can use following workaround for now:

$value = mysql_fetch_object($result)->valueIndex;

While array derefencing wasn't a planned PHP feature, object access is always possible for function results.

This should work shouldn't it?

$value = (mysql_fetch_assoc($result))['valueIndex'];