php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #14763 Make [] of arrays readable
Submitted: 2001-12-30 04:29 UTC Modified: 2001-12-30 10:29 UTC
From: linus at mccabe dot nu Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.1.1 OS:
Private report: No CVE-ID: None
 [2001-12-30 04:29 UTC] linus at mccabe dot nu
Currently in php, you can append to a variable by using the [] form:

$a[] = 'a';
$a[] = 'b';

It would be great if you could also read an array in the same manner, ie:

$lastadded = $a[];

which would be equivalent to 
$lastadded = $a[count($a)-1];
which is what you have to do today.

/linus

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-12-30 04:45 UTC] georg@php.net
There is no need to make an array '$a[]' readable.

$a[] = "foo";

is short for

$a[count($a)] = "foo";

if $a[] would be readable, result would be always NULL:

$b = $a[] ( which would be the same as $a[count($a)])

 [2001-12-30 09:42 UTC] linus at mccabe dot nu
$b = $a[] 
would be
$b = $a[count($a)-1], not $b = $a[count($a)]



 [2001-12-30 10:29 UTC] georg@php.net
In first case, when you write to the array "[]" is a 
synonym for count($array)

In second case, if you read from array "[]" should be 
(your suggestion/feature request) a synonym for 
count($array) - 1.

Sorry, but this is not consistent.

 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Jan 25 04:03:37 2022 UTC