[英]String comparison is not working in php
I'm very new to php. 我对php很陌生。 I have a json named
json
. 我有一个名为
json
。 When I try to do this: 当我尝试这样做时:
echo $json->status;
I get : 我得到:
CREATED
I try to compare this result with normal string CREATED
like this: 我尝试将此结果与普通字符串
CREATED
进行比较,如下所示:
if(strcasecmp("CREATED",$json->status))
{
print_r("Order created successfuly");
}
but for some reason the if condition
is not evaluting to true
. 但是由于某些原因,
if condition
不能评估为true
。 Even though I compare CREATED
with CREATED
! 即使我将
CREATED
与CREATED
比较!
Not sure where the error is. 不知道错误在哪里。
Thanks in advance. 提前致谢。
如果字符串相等,此函数返回零
if (strcasecmp("CREATED",$json->status) == 0)
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
so strcasecmp('a','a')
is 0
, therefore you have to change your code into 因此
strcasecmp('a','a')
为0
,因此您必须将代码更改为
if(strcasecmp("CREATED",$json->status) == 0)
{
print_r("Order created successfuly");
}
http://php.net/manual/en/function.strcasecmp.php http://php.net/manual/zh/function.strcasecmp.php
Quote from the page : 从页面引用:
Returns < 0 if str1 is less than str2;
如果str1小于str2,则返回<0;否则,返回0。 > 0 if str1 is greater than str2, and 0 if they are equal.
如果str1大于str2,则> 0;如果相等,则> 0。
So strcasecmp('CREATED', 'CREATED')
returns 0
. 因此
strcasecmp('CREATED', 'CREATED')
返回0
。 And 0
is not equals to true
. 并且
0
不等于true
。 You must do that : 您必须这样做:
if (strcasecmp("CREATED",$json->status) === 0) {
print_r("Order created successfuly");
}
if (strcasecmp( $json->status, "CREATED") == 0)
{
...
...
}
Why cant you just use a simpler if statement? 为什么不能只使用简单的if语句?
if( $json->status == "CREATED" ) {
print_r("Order created successfuly");
}
And check for whitespaces at the end or start of the status. 并在状态的结尾或开头检查空格。
To compare strings, try to do the following : 要比较字符串,请尝试执行以下操作:
if($json->status == "CREATED")
{
echo "Order created successfuly";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.