English 中文(简体)
开关报表不等于零?
原标题:switch statement will not equate zero?

我的以下职能是适当的,但你使用零件投入值的情况除外。 我试图寻找一下,看看0是否等同于民族解放军,或我是否做过错。

当投入为零时,产出就会超过20项。 谁能解释? 增 编

我计划使转换等于0-10、11-20、21-30、31-40、41+,但仅采用两种情况。 增 编

** 本文件迟交。 我确实在20时想要价值观:

function strengthPlan( $data ) {

    $data = ( int ) $data;
    switch( $data ) {
        case( $data <= 19 ):
            $result =  standard strength ;
            break;
        case( $data >= 20 ):
            $result =  advanced strength ;
            break;
    }
    return $result;

}

echo strengthPlan( 0 );
最佳回答

Your logic is incorrect. Switch statements are checking for equality. Your code is checking whether $data is equal to TRUE or FALSE.

 case( $data < 20 ):

will evaluate to:

 case( TRUE ):

because 0 < 20.

由于0并不等于TRUE,而是等于FALSE(在转换之后),第二种情况是正常的。

基本上,您不能使用<代码>switch case for < or >,但仅限=

问题回答

这并不是要改变发言。 它们将案件与你提供的<代码>switch的价值相比较。 你们应该做的是:

function strengthPlan($length) {
    return $length >= 20 ?  advanced strength  :  standard strength ;
}

如果你重新规划使用更多条件,则使用if...elseif说明如下:

function strengthPlan($length) {
    if     ($length <  5) return  micro strength ;
    elseif ($length < 10) return  tiny strength ;
    elseif ($length < 15) return  small strength ;
    elseif ($length < 20) return  standard strength ;
    elseif ($length < 30) return  advanced strength ;
    else                  return  super strength!!!!! ;
}

它将 each下每一条条件,直到它打上了一定数量。 或者,你可以采用某种看像样的表格:

function strengthPlan($length) {
    $plans = array(
        1            =>  super strength!!!!! ,
        $length < 30 =>  advanced strength ,
        $length < 20 =>  standard strength ,
        $length < 15 =>  small strength ,
        $length < 10 =>  tiny strength ,
        $length < 5  =>  micro strength ,
    );
    return $plans[1];
}

就此进行了讨论:

Using switch in this case is wrong

使用

http://sand Box.phpcode.eu/g/b4053.php

<?php 
function strengthPlan( $data ) { 
    return ($data > 20) ?  advanced strenght  :  standard strenght ; 
} 

echo strengthPlan( 0 );




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签