Req #70378 [Opn]: keyval (=effective value as array key) to complement strval/intval
[email protected] ("rn at alpha9marketing dot com")
| Newsgroups | php.standards |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=70378&edit=1
ID: 70378
User updated by: rn at alpha9marketing dot com
Reported by: rn at alpha9marketing dot com
Summary: keyval (=effective value as array key) to complement
strval/intval
Status: Open
Type: Feature/Change Request
Package: PHP Language Specification
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Also of note the float-as-string-as-key case, as opposed to the actual float-as-key:
php > $a = array("1.234" => 'kittycat');
php > var_dump($a);
array(1) {
["1.234"]=>
string(8) "kittycat"
}
I.e. one would expect
php > var_dump(keyval("1.234"));
string(5) "1.234"
Previous Comments:
------------------------------------------------------------------------
[2015-08-28 08:52:32] rn at alpha9marketing dot com
Description:
------------
PHP offers strval, intval, floatval, doubleval to manually coerce values to desired types. This eases strict comparisons with the === operator within a desired type domain.
One omission is that one can not trivially cast a value as it would be (internally) when that value is used as an array key.
PHP apparently casts booleans to integers when they are used as array keys. A subset of numeric strings (minus hex / octal literal strings, potentially others) is cast to integers, as are floats (truncated towards zero).
NULL is converted to an empty string.
There is currently no built-in function that exposes this special type coersion used for array keys to the programmer, though one would have to assume it exists somewhere in the PHP code and exposing it would require only an external interface without (much) additional logic.
Expected result:
----------------
php > var_dump(keyval(NULL));
string(0) ""
php > var_dump(keyval(false));
int(0)
php > var_dump(keyval(true));
int(1)
php > var_dump(keyval(-1.734));
int(-1)
php > var_dump(keyval(1.734));
int(1)
php > var_dump(keyval("12"));
int(12)
php > var_dump(keyval("0xA"));
string(3) "0xA"
php > var_dump(keyval("017"));
string(3) "017"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70378&edit=1