php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17655 key and intval on associative array are equal
Submitted: 2002-06-08 09:04 UTC Modified: 2002-06-08 10:25 UTC
From: msopacua at idg dot nl Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.2.1 OS: Any
Private report: No CVE-ID: None
 [2002-06-08 09:04 UTC] msopacua at idg dot nl
Sample:
<?php
$arr = array(
	'string'	=> 'a string',
	'anotherstring'	=> 'b string'
);

print(key($arr)."\t");
print(intval(key($arr))."\n");
if(key($arr) == intval(key($arr)))
{
	print("Equal!\n");
}
else
{
	print("Not equal\n");
}
if(key($arr) === intval(key($arr)))
{
	print("Equal!\n");
}
else
{
	print("Not equal\n");
}
?>

Output:
string	0
Equal!
Not equal

That really defeats the use of intval().
Confirmed on BSD/OS and Native windows/isapi.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-08 10:25 UTC] mfischer@php.net
Sorry, but the bug system is not the appropriate forum for asking
support questions. Your problem does not imply a bug in PHP itself.
For a list of more appropriate places to ask for help using PHP,
please visit http://www.php.net/support.php

Thank you for your interest in PHP.

Makes perfectly sense to me. key($arr) returns a "string". intval() returns an int. The first if() tests string == int. PHP automatically converts the string to an integer (which is 0 because there's no valid number in key($arr) ). The second if() compares also the "types" of the variables which do not match of course.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Dec 13 20:01:23 2019 UTC