php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77286 The array key like '123' Not a string?
Submitted: 2018-12-12 04:07 UTC Modified: 2018-12-12 04:15 UTC
From: xpigeon at 163 dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.3.0 OS: Windows 7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
14 + 1 = ?
Subscribe to this entry?

 
 [2018-12-12 04:07 UTC] xpigeon at 163 dot com
Description:
------------
In the array like this:
```
'123'=>'abc'
```
The key like '123' Not as a string?


Test script:
---------------
<?php
$value = 'mystr-abcd-1234';
$keys = ['123'=>0, '456'=>0, '789'=>0, 'abc'=>0, 'xyz'=>0];
foreach($keys as $key=>$v){
	if(strstr($value,"$key")){ // is OK
	if(strstr($value,$key)){ // Notice Error
		$keys[$key]=1;
	}
}
var_dump($keys);
die();
/*
php7.3
strstr(): Non-string needles will be interpreted as strings in the future. 
Use an explicit chr() call to preserve the current behavior
*/
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-12-12 04:10 UTC] xpigeon at 163 dot com
This line will Annotated:
```
if(strstr($value,"$key")){ // is OK
```
-=> Edit as
```
//if(strstr($value,"$key")){ // is OK
```
 [2018-12-12 04:15 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-12-12 04:15 UTC] requinix@php.net
http://php.net/manual/zh/language.types.array.php
> 包含有合法整型值的字符串会被转换为整型。例如键名 "8" 实际会被储存为 8。但是 "08" 则不会强制转换,因为其不是一个合法的十进制数值。

var_dump($key) -> int(123), int(456), int(789), string(3) "abc", string(3) "xyz"
 [2018-12-12 04:43 UTC] xpigeon at 163 dot com
Thanks!
I have this code in a project, it runs ok in php7.2;
But in php7.3, it show Notice Error, So I report is as a bug!

您从我的邮箱及蹩脚的英文中猜出我是来中国!?
再次感谢!
 [2018-12-12 05:41 UTC] a at b dot c dot de
It's not a bug if it's a deliberate and documented change.

http://www.php.net/manual/zh/migration73.deprecated.php#migration73.deprecated.core.string-search
 [2018-12-12 05:55 UTC] xpigeon at 163 dot com
to: a at b dot c dot de

Yeah! I just want find this!
But indeed, I read this document,
But I was NOT read it carefully!
 [2018-12-12 10:48 UTC] spam2 at rhsoft dot net
FWIW with strict-types that code has been a type error for years

<?php declare(strict_types=1);
$value = 'mystr-abcd-1234';
$keys = ['123'=>0, '456'=>0, '789'=>0, 'abc'=>0, 'xyz'=>0];
foreach($keys as $key=>$value)
{
 if(strstr($value,$key))
 {
  $keys[$key]=1;
 }
}
?>

Fatal error: Uncaught TypeError: strstr() expects parameter 1 to be string, integer given in /mnt/data/downloads/test.php:6
Stack trace:
#0 /mnt/data/downloads/test.php(6): strstr(0, 123)
#1 {main}
  thrown in /mnt/data/downloads/test.php on line 6
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 15:01:29 2024 UTC