为什么 echoempty($location) 打印出 1 对应的语句没有执行

发布于 2024-08-14 17:52:27 字数 331 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥繁 2024-08-21 17:52:28

empty 函数返回布尔值 1 或 0 https://www.php。 net/empty

switch / case 语句检查变量是否包含某个值并根据该值执行表达式。

在您的情况下,表达式 1 应该是如果 $location==1 的值被执行(当你输入 switch($location) 时,你实际上要求了这个值),

所以上面代码的逻辑是:

如果$location==1,则执行表达式1
if $location%10000==0, 执行表达式2
等等等等

$location==1的值吗?

The empty function returns a boolean value of 1 or 0 https://www.php.net/empty

Whereas the switch / case statements check whether a variable holds a certain value and execute an expression depending on that

In you case, expression 1 should be executed if the value of $location==1 (you effectively asked for that when you typed switch($location)),

So the logic of your above code is:

if $location==1, execute expression 1
if $location%10000==0, execute expression 2
etc. etc.

is the value of $location==1?

从此见与不见 2024-08-21 17:52:28

switch 语句与 if/else 语句不同。 Switch 语句正在寻找特定值。如果它找到给定 case 语句中指定的值,它将运行该 case 语句之后的代码。

以下代码:

switch($x)
    case 1:
        // some stuff
        break;
    case 2:
        // some other stuff
        break;
    default:
        // some more stuff
        break;

与此代码等效:

if($x == 1){
    // some stuff
}
elseif($x == 2){
    // some other stuff
}
else{
    // some more stuff
}

基本上,switch 语句是 if/elseif/else 块的快捷方式,您可以在其中检查单个变量与一堆可能性。

由于 empty() 返回 0 或 1,因此如果 $location 为 1(如果 $location > 为空)或 0(如果 $location 不为空)。这几乎就像您写了以下内容:

elseif($location == empty($location)){ ...

有意义吗?您可能需要以下内容,而不是使用 switch 语句:

if(empty($location)){
    // ...
}
elseif($location % 10000 == 0){
    // ...
}
// ...

A switch statement is not the same as an if/else statement. Switch statements are looking for specific values. If it finds the value specified in a given case statement, it runs the code after that case statement.

The following code:

switch($x)
    case 1:
        // some stuff
        break;
    case 2:
        // some other stuff
        break;
    default:
        // some more stuff
        break;

Is the equivalent of this code:

if($x == 1){
    // some stuff
}
elseif($x == 2){
    // some other stuff
}
else{
    // some more stuff
}

Basically, switch statements are shortcuts for if/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 first case 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:

elseif($location == empty($location)){ ...

Make sense? Instead of using a switch statement, you probably want the following:

if(empty($location)){
    // ...
}
elseif($location % 10000 == 0){
    // ...
}
// ...
烟凡古楼 2024-08-21 17:52:27

您没有正确使用 switch 语句。它们的工作方式是将每个 case 值与初始 switch 值进行比较。

对于您的情况,让我们假设 $location = null;

echo empty($location);    // true: null is considered empty.

switch ($location) {
    case empty($location) :    // this performs the check:
                               // $location == empty($location)
                               //      null == true ==> false

所以这就是它不运行的原因。

在这种情况下,我建议坚持使用 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;

echo empty($location);    // true: null is considered empty.

switch ($location) {
    case empty($location) :    // this performs the check:
                               // $location == empty($location)
                               //      null == true ==> false

so that's why it doesn't run..

I'd recommend sticking to if .. else in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文