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
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.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 17:01:29 2024 UTC