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
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 27 04:03:38 2021 UTC