Home >Backend Development >PHP Tutorial >奇异的php 语法, 求解

奇异的php 语法, 求解

WBOY
WBOYOriginal
2016-06-13 12:02:281093browse

怪异的php 语法, 求解!

发现一个php用来判断一个字符串是否包子串的返回值与方法 strpos相当的怪异.

看下面的语句:

echo "A1: ".(strpos("csd","c"))."
";   //0

echo "A2: ".(strpos("csd","c")>=0)."
"; //1

echo "B1: ".(strpos("csd","h",0))."
"; //
echo "B2: ".(strpos("csd","h",0)>=0)."
"; //1


//期望值是源串"abc"包含子串"a", 此处结果与期望值相同
if(strpos("abc","a")>=0)
{
    echo "a in abc"."
"; //a in abc

}
else
{
    echo "a not in abc"."
";
}


//期望值是源串"abc"并不包含子串"u", 此处结果与期望值相反

if(strpos("abc","u")>=0)
{
    echo "u in abc"."
"; //
u in abc
}
else
{
    echo "u not in abc"."
";
}



//期望值是源串"a"比子串"abc"小,并且源串"a"并不包含子串"abc", 此处结果与期望值相反

if(strpos("a","abc")>=0)
{
    echo "abc in a"."
"; //abc in a

}
else
{
    echo "abc not in a"."
";
}



//期望值是源串"u"比子串"abc"小,并且源串"u"并不包含子串"abc", 此处结果与期望值相反

if(strpos("u","abc")>=0)
{
    echo "abc in u"."
"; //abc in u

}
else
{
    echo "abc not in u"."
";
}


//显示的结果

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 可选。规定开始搜索的位置。


PHP的这种判断方法与 C#, JAVA的很不一样.
php里是把源串不包子串,源串小于子串当做空值返回.
经过条件语句,if(strpos("abc","u")>=0) 和 if(strpos("u","abc")>=0), 值又为真了.
我觉得这种形式非常的怪异,初学者和从其它语言(JAVA,C#)转过来学习php的人应该会感到很困惑..

请教,在这里,比如我要判断 字符串"abc" 里包含 "a", 用哪个方法比较好呢?


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn