php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27475 Comparison fails for int(0) == string
Submitted: 2004-03-02 23:01 UTC Modified: 2004-03-02 23:07 UTC
From: tim dot lokot at printsoft dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.4 OS: Win2K Pro
Private report: No CVE-ID: None
 [2004-03-02 23:01 UTC] tim dot lokot at printsoft dot com
Description:
------------
For some reason I cannot compare the integer zero to a string and get a valid response back.  There are two ways that I can see to fix it:

1. Use the === operator
2. Typecast the integer to a string

Both of the above solutions work, yet for some reason, the == comparison operator doesn't.

Reproduce code:
---------------
<?php

	$item = 0;

	var_dump ($item);

	if ($item == 'rubbish')
		echo 'Equals';
	else
		echo 'Is not equal';

?>

Expected result:
----------------
int(0)
Is not equal

Actual result:
--------------
int(0)
Equals

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-02 23:07 UTC] tim dot lokot at printsoft dot com
Just found this in the documentation ...

"The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero)."

Not quite sure why the value zero was chosen, but hey, it's in the manual so I can live with that.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Aug 17 17:01:29 2024 UTC