php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54625 string is equals a 0 !?
Submitted: 2011-04-28 17:32 UTC Modified: 2011-04-28 17:45 UTC
From: bernardo at datamex dot com dot br Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: Irrelevant OS: Windows and Freebsd
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: bernardo at datamex dot com dot br
New email:
PHP Version: OS:

 

 [2011-04-28 17:32 UTC] bernardo at datamex dot com dot br
Description:
------------
// tested both in
// in windows 7 - php 5.2.12.12
// in freebsd 7 - php 5.3.3

// because 'e' equals zero, zero is false and false is not equal to 'e'

var_dump(0 == 'e'); // true
var_dump(false == 0); // true
var_dump(false == 'e'); // false
echo '<hr>';

// this means that when we compare int to string will always be equal?

var_dump(0 == "e"); // true
var_dump(0 == "Bernardo"); // true

what is the meaning of this? I think it should not return true.


Test script:
---------------
<?
var_dump(0 == 'e'); // true
?>

Expected result:
----------------
bool(false) 

Actual result:
--------------
bool(true) 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-28 17:39 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2011-04-28 17:39 UTC] johannes@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

See http://php.net/manual/en/types.comparisons.php
 [2011-04-28 17:45 UTC] rasmus@php.net
Use === to do a strict equality check. == does type juggling
because the Web isn't typed. Everything you get from a Web 
browser is a string so PHP makes sure that 1=='1'. 

In your example, you are comparing variables of different
types, so PHP juggles them to match:

The integer value of string 'e' is zero
The integer value of boolean false is zero
The string value of false is '' which is not 'e'
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 07:01:27 2024 UTC