search
HomeBackend DevelopmentPHP Tutorial奇异的php 语法, 求解

怪异的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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.