php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #10678 distinction between false and "0" (string)
Submitted: 2001-05-04 19:21 UTC Modified: 2001-05-04 20:11 UTC
From: ms at marcant dot net Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.0.4pl1 OS:
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: ms at marcant dot net
New email:
PHP Version: OS:

 

 [2001-05-04 19:21 UTC] ms at marcant dot net
There is a subtle problem with functions returning 'false' as
an return status indication.

Take for example strstr(haystack, needle). 

The function returns 'false' to indicate that <needle> could not
be found in <haystack> or returns the substring of haystack
starting with <needle>

With certain strings the above assertion is not true with php:

$haystack='1230';

if (strstr($haystack,"0")==false)
        { echo ' oh no.. '; }


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-05-04 19:34 UTC] torben@php.net
This has been discussed several times on the mailing lists;
check the archives at http://marc.theaimsgroup.com.

The answer is in the manual: 

http://www.php.net/manual/en/language.operators.comparison.php

Basically, 0 is a false value but is of type int. You want
to test for a return value of false (type boolean), so you
need the 'identical' comparison operator: '==='.

$haystack = '1230';
if (strstr($haystack, '0') === false) {
    echo "oh no\n";
}

 [2001-05-04 20:11 UTC] ms at marcant dot net
ah .. sorry. thanks for the explanation.

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed May 07 20:01:46 2025 UTC