[英]elseif value is less than getting value or greater than
I am trying to show echo 2 but its not working我正在尝试显示 echo 2 但它不起作用
$zipcode1 = 07300-011;
$zipcode= str_replace("-","",$zipcode1);
$zipcode = 07300011;
if ($zipcode >= 20000000 && $zipcode <= 26600999) {
echo '1';
}
elseif ($zipcode >= 07000001 && $zipcode <= 07399999) {
echo '2';
}
else {
echo 'no value';
}
Please let me know where i am doing wrong.请让我知道我哪里做错了。 Thank you for the help Result = 2谢谢你的帮助 结果 = 2
You need to compare string with string if the leading 0 of the zipcode is important:如果邮政编码的前导 0 很重要,则需要将字符串与字符串进行比较:
$zipcode1 = "07300-011";
$zipcode= str_replace("-","",$zipcode1);
$zipcode = "07300011";
if ($zipcode >= "20000000" && $zipcode <= "26600999") {
echo '1';
} elseif ($zipcode >= "07000001" && $zipcode <= "07399999") {
echo '2';
} else {
echo 'no value';
}
You made two mistakes.你犯了两个错误。
One is that the value assigned to $zipcode1
is not a string, but rather the result of an arithmetic operation.一个是分配给$zipcode1
的值不是字符串,而是算术运算的结果。 I'm saying $zipcode1
is not "07300-011"
, but rather 07300-011
, which is equal to the octal number 7300
( 3776
in base 10) minus octal 11
( 9
in base 10), ie 3776 - 9
which is 3767
.我是说$zipcode1
不是"07300-011"
,而是07300-011
,它等于八进制数7300
(基数 10 中的3776
)减去八进制数11
(基数 10 中的9
),即3776 - 9
这是3767
。
The second is that you're trying to do a numeric comparison using strings.第二个是您正在尝试使用字符串进行数字比较。 "20" > "1000"
is not the same as 20 > 1000
. "20" > "1000"
与20 > 1000
不同。 The first would resolve to true
, whereas the second would give false
(which is likely what you want).第一个将解析为true
,而第二个将给出false
(这可能是您想要的)。 To fix this, you have to first convert both of them to numbers.要解决此问题,您必须先将它们都转换为数字。 You can either cast them:你可以投他们:
((int) $zipcode1) > (int) $zipcode2
or you can use the +
sign instead:或者您可以改用+
号:
(+$zipcode1) > (+$zipcode2)
In both cases, you need to first remove whitespaces and every other non-numeric character from the zipcode.在这两种情况下,您都需要先从邮政编码中删除空格和所有其他非数字字符。
$zipcode = str_replace([' ', '-'], '', $zipcode);
Read the following topics in the php docs for more info:阅读 php 文档中的以下主题以获取更多信息:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.