php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #30419 strpos does not autocast integer needles
Submitted: 2004-10-13 08:20 UTC Modified: 2004-10-13 13:38 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: m dot konings at knowmany dot nl Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.8 OS: Windows XP SP2
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: m dot konings at knowmany dot nl
New email:
PHP Version: OS:

 

 [2004-10-13 08:20 UTC] m dot konings at knowmany dot nl
Description:
------------
When I use a integer variable as needle to the strpos() function, the needle is not found.

Reproduce code:
---------------
<?
$str_haystack = '12345';
$str_needle = '3';
$int_haystack = 12345;
$int_needle = 3;

$test = strpos($str_haystack, $str_needle);
var_dump($test);

$test = strpos($str_haystack, $int_needle);
var_dump($test);

$test = strpos($int_haystack, $str_needle);
var_dump($test);

$test = strpos($int_haystack, $int_needle);
var_dump($test);
?>

Expected result:
----------------
Due to the fact the strpos() is declared as int strpos ( string haystack, string needle [, int offset] )

I would expect that using a integer as needle would cause a implicit type cast to string. This does not seem to be the case, because using an integer as needle for strpos() will always return false (bool).

Actual result:
--------------
The call to strpos() with the integer needle return false (bool), while the call to strpos() with string needles return the actual position within the haystack.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-13 09:24 UTC] derick@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 the manual (http://php.net/strpos):
   If needle is not a string, it is converted to an integer
   and applied as the ordinal value of a character.
 [2004-10-13 09:30 UTC] m dot konings at knowmany dot nl
Thanks for the explanation. Although I feel that in this case the documenation should reflect that strpos() takes a mixed needle variable type, not a string. There are lots of other PHP functions that take mixed variable types as parameter, and for those, the manual states so in the function declaration.
 [2004-10-13 09:59 UTC] derick@php.net
I agree with that, making it a documentation problem.
 [2004-10-13 13:23 UTC] aidan@php.net
I'm not sure I agree with that. Almost all the functions take scalar values, but the prefered (or optimal) is listed.

PHP is loosely typed, I can't think of many situations were "1" instead of 1 will not work as expected (there are a few of course), I don't think these declarations should be changed to mixed either.
 [2004-10-13 13:26 UTC] derick@php.net
True, but it should be done here becuase it makes the function behave in two TOTALLY different ways.
 [2004-10-13 13:38 UTC] aidan@php.net
All fixed!
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Aug 17 18:00:03 2025 UTC