php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72546 Unexpected implicit cast to number
Submitted: 2016-07-05 12:13 UTC Modified: 2016-07-05 12:21 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: account at friends-of-bytes dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: account at friends-of-bytes dot com
New email:
PHP Version: OS:

 

 [2016-07-05 12:13 UTC] account at friends-of-bytes dot com
Description:
------------
There is an array with 3 values. These values are referenced by 3 string-keys. 

It seems that there is a implicit cast with the index '0' to int, so that
(0 == "any string") is always true. 

but:
('0' == "any string") is always false, so there is no reason for that result.

Test script:
---------------
$kat['test'] = "a";
$kat['0'] = "b";
$kat['1'] = "c"; 
   
foreach ($kat as $key => $val)
{
   if ($key != "test")
      $i++;
}
echo $i;

Expected result:
----------------
2

Actual result:
--------------
1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-05 12:21 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-07-05 12:21 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

String keys that are valid integers are converted to integers. That "0" key is actually just 0.
http://php.net/manual/en/language.types.array.php#language.types.array.syntax.array-func
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 15:01:36 2025 UTC