php比较字符串结果
我如何将 if else 语句与 if else 语句进行比较。在 mysql 查询中我可以使用 LIKE %
语句。
我想将字符串结果与 php.ini 进行比较。 即:Pro Tan 3 分钟、Pro Tan 6 分钟、Pro Tan 9 分钟
在 mysql 中:
$db->query("SELECT * FROM treat WHERE tanning LIKE 'Pro Tan%'");
在 php 中:
if($tanning == 'Pro Tan') :
echo 'xxx';
else :
echo 'zzz';
endif
// output zzz
how do i compare the with if else statement. in mysql query i can use LIKE %
statement.
i want to compare the string result with php.
ie: Pro Tan 3mins, Pro Tan 6mins, Pro Tan 9mins
in mysql :
$db->query("SELECT * FROM treat WHERE tanning LIKE 'Pro Tan%'");
in php:
if($tanning == 'Pro Tan') :
echo 'xxx';
else :
echo 'zzz';
endif
// output zzz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用多种不同的比较函数,具体取决于您想要进行比较的复杂程度。要专门查找以“Pro Tan”开头的字符串,您可以这样做:
注意三重
===
,因为 ifstrpos
返回false
,这与返回0
完全不同。There are a variety of different comparison functions you can use, depending on how sophisticated you want to make your comparison. To find specifically a string that starts with "Pro Tan", you could do:
Note the triple
===
, since ifstrpos
returnsfalse
, that's not at all the same as returning0
.假设结果以值开头,您可以使用
strncmp
。对于不区分大小写,您还可以使用
strncasecmp
。Assuming the result begins with the value, you can use
strncmp
.For case-insensative, you can also use
strncasecmp
.您正在寻找 PHP 中的
strstr()
函数,它允许您搜索字符串中的字符串。You are looking for the
strstr()
function in PHP, which allows you to search for a string within a string.给出的其他答案也将起作用,但使用
preg_match()
的正则表达式更加灵活,并且将继续适用于更复杂的匹配。(有人会说正则表达式很慢,但在这种情况下,因为它只是匹配锚定到开头的直字符串,所以这是一个非常有效的查询)
Other answers given will also work, but regular expressions using
preg_match()
is more flexible and will continue to work with more complex matching.(some will say that regex is slow, but in this case, since it's just matching a straight string anchored to the start, it's a very efficient query)