Home > Article > Backend Development > 怪异的php 语法, 求解!
发现一个php用来判断一个字符串是否包子串的语句相当的怪异.
看下面的语句:
echo "A1: ".(strpos("csd","c"))."
";
echo "A2: ".(strpos("csd","c")>=0)."
";
echo "B1: ".(strpos("csd","h",0))."
";
echo "B2: ".(strpos("csd","h",0)>=0)."
";
if(strpos("abc","a")>=0)
{
echo "a in abc"."
";
}
else
{
echo "a not in abc"."
";
}
if(strpos("abc","u")>=0)
{
echo "u in abc"."
";
}
else
{
echo "u not in abc"."
";
}
if(strpos("a","abc")>=0)
{
echo "abc in a"."
";
}
else
{
echo "abc not in a"."
";
}
if(strpos("u","abc")>=0)
{
echo "abc in u"."
";
}
else
{
echo "abc not in u"."
";
}
strpos
//显示的结果
A1: 0
A2: 1
B1:
B2: 1
a in abc
u in abc
abc in a
abc in u
strpos() 函数返回字符串在另一个字符串中第一次出现的位置。
如果没有找到该字符串,则返回 false。
strpos(string,find,start)
string | 必需。规定被搜索的字符串。 |
find | 必需。规定要查找的字符。 |
start | 可选。规定开始搜索的位置。 |
请教,在这里,比如我要判断 字符串"abc" 里包含 "a", 用哪个方法比较好呢?