为什么 echoempty($location) 打印出 1 对应的语句没有执行
echo empty($location);
switch($location){
case (empty($location)):
expression 1;
break;
case ($location%10000==0):
expression 2;
break;
case ($location%100==0):
expression 3;
break;
default:
expression 4;
break;
}
当我 echoempty($location) 时,它打印出 1,为什么表达式 1 没有执行?
echo empty($location);
switch($location){
case (empty($location)):
expression 1;
break;
case ($location%10000==0):
expression 2;
break;
case ($location%100==0):
expression 3;
break;
default:
expression 4;
break;
}
When I echo empty($location), it prints out 1, why is expression 1 not executed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
empty
函数返回布尔值 1 或 0 https://www.php。 net/empty而
switch / case
语句检查变量是否包含某个值并根据该值执行表达式。在您的情况下,
表达式 1
应该是如果$location==1
的值被执行(当你输入switch($location)
时,你实际上要求了这个值),所以上面代码的逻辑是:
是
$location==1
的值吗?The
empty
function returns a boolean value of 1 or 0 https://www.php.net/emptyWhereas the
switch / case
statements check whether a variable holds a certain value and execute an expression depending on thatIn you case,
expression 1
should be executed if the value of$location==1
(you effectively asked for that when you typedswitch($location)
),So the logic of your above code is:
is the value of
$location==1
?switch
语句与if/else
语句不同。 Switch 语句正在寻找特定值。如果它找到给定case
语句中指定的值,它将运行该case
语句之后的代码。以下代码:
与此代码等效:
基本上,
switch
语句是if/elseif/else
块的快捷方式,您可以在其中检查单个变量与一堆可能性。由于
empty()
返回 0 或 1,因此如果$location
为 1(如果$location
> 为空)或 0(如果$location
不为空)。这几乎就像您写了以下内容:有意义吗?您可能需要以下内容,而不是使用
switch
语句:A
switch
statement is not the same as anif/else
statement. Switch statements are looking for specific values. If it finds the value specified in a givencase
statement, it runs the code after thatcase
statement.The following code:
Is the equivalent of this code:
Basically,
switch
statements are shortcuts forif/elseif/else
blocks where you're checking for a single variable's equality against a bunch of possibilities.Since
empty()
returns 0 or 1, your firstcase
will run if$location
is 1 (if$location
is empty) or 0 (if$location
isn't empty). It's almost like you've written the following:Make sense? Instead of using a
switch
statement, you probably want the following:您没有正确使用 switch 语句。它们的工作方式是将每个 case 值与初始
switch
值进行比较。对于您的情况,让我们假设
$location = null
;所以这就是它不运行的原因。
在这种情况下,我建议坚持使用
if .. else
。You're not using switch statements properly. The way they work is to compare each case value against the initial
switch
value.In your case, let's pretend
$location = null
;so that's why it doesn't run..
I'd recommend sticking to
if .. else
in this case.