PHP 7.3 strpos() 针问题?
PHP 7.3 strpos() needles issue?
ERROR:strpos(): 非字符串针以后会被解释为字符串。使用显式 chr() 调用来保留
中的当前行为
阅读了大量关于此的在线主题 - none 其中似乎涉及到我的代码,这让我不确定我哪里错了。
此代码使用 get 来搜索文本文件,returns 使用该搜索的文件数组。代码一直运行到 7.3 更新。
$search_get = $_GET['q'];
if ($search_get = NULL) {
$search_get = 'encyclopedia';
}
foreach (glob("dir/*.txt") as $search) {
$contents = file_get_contents($search);
if (!strpos($contents, $search_get)) continue;
$found[] = $search;
}
$search_get 怎么在这里无效?
@paul-t 是正确的,您正在分配变量而不是将其与 null 进行比较,这就是为什么您应该使用 so-called Yoda conditions
if (null === $search_get) {
$search_get = 'encyclopedia';
}
无论如何,这是您的代码的稍微简化的版本:
$search_get = @$_GET['q'] ?: 'encyclopedia';
foreach (glob("dir/*.txt") as $search) {
$contents = file_get_contents($search);
if (!empty($contents) && false !== strpos($contents, $search_get)) {
$found[] = $search;
}
}
顺便说一句,请考虑使用 stripos
而不是 strpos
,因为目前您对 $contents
变量的检查是 case-sensitive.
ERROR:strpos(): 非字符串针以后会被解释为字符串。使用显式 chr() 调用来保留
中的当前行为阅读了大量关于此的在线主题 - none 其中似乎涉及到我的代码,这让我不确定我哪里错了。
此代码使用 get 来搜索文本文件,returns 使用该搜索的文件数组。代码一直运行到 7.3 更新。
$search_get = $_GET['q'];
if ($search_get = NULL) {
$search_get = 'encyclopedia';
}
foreach (glob("dir/*.txt") as $search) {
$contents = file_get_contents($search);
if (!strpos($contents, $search_get)) continue;
$found[] = $search;
}
$search_get 怎么在这里无效?
@paul-t 是正确的,您正在分配变量而不是将其与 null 进行比较,这就是为什么您应该使用 so-called Yoda conditions
if (null === $search_get) {
$search_get = 'encyclopedia';
}
无论如何,这是您的代码的稍微简化的版本:
$search_get = @$_GET['q'] ?: 'encyclopedia';
foreach (glob("dir/*.txt") as $search) {
$contents = file_get_contents($search);
if (!empty($contents) && false !== strpos($contents, $search_get)) {
$found[] = $search;
}
}
顺便说一句,请考虑使用 stripos
而不是 strpos
,因为目前您对 $contents
变量的检查是 case-sensitive.