strpos 不适用于某个字符串
strpos not working for a certain string
我正在尝试使用 strpos 在另一个字符串中查找一个字符串,但由于某种原因它不适用于某个字符串,即使它对另一个字符串有效。我做错了什么?
<?php
if (strpos("show me how to dance", "show me")) {
echo "true1";
}
if (strpos("my name is name", "name")) {
echo "true2";
}
?>
结果:
true2
预期结果:
true1true2
strpos
returns 字符串中出现的索引(如果未找到则为 false)。当此索引为 0 时,条件:(strpos("show me how to dance", "show me"))
被评估为假(因为在 PHP 中:0 == false
为真)。为确保找到针(即使在索引 0 处),您需要使用严格比较:
if (strpos("show me how to dance", "show me") !== false)
我正在尝试使用 strpos 在另一个字符串中查找一个字符串,但由于某种原因它不适用于某个字符串,即使它对另一个字符串有效。我做错了什么?
<?php
if (strpos("show me how to dance", "show me")) {
echo "true1";
}
if (strpos("my name is name", "name")) {
echo "true2";
}
?>
结果:
true2
预期结果:
true1true2
strpos
returns 字符串中出现的索引(如果未找到则为 false)。当此索引为 0 时,条件:(strpos("show me how to dance", "show me"))
被评估为假(因为在 PHP 中:0 == false
为真)。为确保找到针(即使在索引 0 处),您需要使用严格比较:
if (strpos("show me how to dance", "show me") !== false)