php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25740 strstr unexpected behavior
Submitted: 2003-10-03 01:49 UTC Modified: 2003-10-03 23:56 UTC
From: daijoubu at videotron dot ca Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.3.3 OS: Win98SE
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: daijoubu at videotron dot ca
New email:
PHP Version: OS:

 

 [2003-10-03 01:49 UTC] daijoubu at videotron dot ca
Description:
------------
string strstr ( string haystack, string needle)

If needle is numeric and lenght of 1 at the end of the haystack, returns false

No problem if needle is 'a' ($test = '123456789a') or '90' ($test = '1234567890')

Reproduce code:
---------------
$test = '1234567890';
if (strstr($test, '0'))
{
    $result = 1;
}

Expected result:
----------------
result = 1;

Should output the same as:
if (strpos($test, '0') !== false)
{
    $result = 1;
}

Actual result:
--------------
$result = 0;

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-10-03 23:56 UTC] sniper@php.net
from the manual page for strstr():
"Returns part of haystack string from the first occurrence of needle to the end of haystack."

strstr() works just fine, the last char in your string is '0' (zero). Try this:

<?php 
$test = '1234567890';
var_dump(strstr($test, '0'));
?>

# php test.php 
string(1) "0"

You see it's string, it's not boolean. 
To get reliable results, always check the type of returned
variable. Like this:

<?php 
  $test = '1234567890';
  if (strstr($test, '0') !== false) {
    $result = 1;
  }
?>

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 05:01:30 2024 UTC