|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70378 keyval (=effective value as array key) to complement strval/intval
Submitted: 2015-08-28 08:52 UTC Modified: 2015-08-28 09:22 UTC
Avg. Score:3.7 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: rn at alpha9marketing dot com Assigned:
Status: Open Package: Variables related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: rn at alpha9marketing dot com
New email:
PHP Version: OS:


 [2015-08-28 08:52 UTC] rn at alpha9marketing dot com
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));
php > var_dump(keyval(true));
php > var_dump(keyval(-1.734));
php > var_dump(keyval(1.734));
php > var_dump(keyval("12"));
php > var_dump(keyval("0xA"));
string(3) "0xA"
php > var_dump(keyval("017"));
string(3) "017"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-28 09:20 UTC] rn at alpha9marketing dot com
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) {
  string(8) "kittycat"

I.e. one would expect
php > var_dump(keyval("1.234"));
string(5) "1.234"
 [2015-08-28 09:22 UTC]
-Package: PHP Language Specification +Package: Variables related
 [2021-09-09 11:55 UTC] prog at alkaan dot com
Same problem when defined keys are integers in quotes and others are strings. Example :

$t = [ '15' => 'first', 'AZ78' => 'second'];

return :

[15=>'first', 'AZ78' => 'second']

This is a problem when we want use strict comparison on keys :

$compare = "15";
foreach($t as $key => $value) {
  if ($key === $compare) {
    return $value;

in this example, we never return $value

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 23 05:01:29 2024 UTC