php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #68341 reset() & end() could return value of arbitrary expression
Submitted: 2014-11-04 10:22 UTC Modified: 2020-12-08 13:13 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: bugs dot php dot net at dzubak dot sk Assigned: cmb (profile)
Status: Closed Package: Arrays related
PHP Version: 5.5.18 OS:
Private report: No CVE-ID: None
 [2014-11-04 10:22 UTC] bugs dot php dot net at dzubak dot sk
Description:
------------
Since PHP 5.5 empty() supports arbitrary expressions, could PHP do same with reset() and end()?

I can simply add [0] to end of variable or function to get value of last element. However this approach does not work when I use arbitrary or non-integer keys. It is even more tricky when I want "just get last value" of array.

Expected result:
----------------
I would welcome to be able to get first and last value of "any kind of array" like this:

reset(someFunction()); # returns value of first element

end(explode('/', '/very/long/path/to/filename.txt')); # returns filename without path (value of last element of explode())

Actual result:
--------------
end([]); # Fatal error: Only variables can be passed by reference

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-05 09:26 UTC] bugs dot php dot net at dzubak dot sk
-Summary: reset() & end() to return value for "not variables" like empty() in PHP 5.5 +Summary: reset() & end() could return value of arbitrary expression
 [2014-11-05 09:26 UTC] bugs dot php dot net at dzubak dot sk
updated summary, hopefully it will make more sense
 [2014-11-06 08:12 UTC] bugs dot php dot net at dzubak dot sk
I made mistake in this sentence:
I can simply add [0] to end of variable or function to get value of **FIRST** element.
 [2020-12-08 13:13 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-12-08 13:13 UTC] cmb@php.net
As of PHP 7.3.0, the functions array_key_first() and
array_key_last() are available, which you can use instead.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 11:01:30 2024 UTC