Php If else声明

时间:2014-10-10 02:42:21

标签: php if-statement

我在这里做错了,返回值不正确,只返回其他部分..

我想检查输入值是大于还是等于或小于或等于下面显示的条件,这是正确的方法,还是我做错了...

$tornadoball = 22;
$tornadoVal = "";
$tornadoScore = "";

if ( $tornadoball <= 20 && $tornadoball >= 29): {
    $tornadoScore = 0.25;
}

elseif ( $tornadoball <= 30 && $tornadoball >= 34): {
    $tornadoScore = 0.50;
}

elseif ( $tornadoball <= 35 && $tornadoball >= 39 ): {
    $tornadoScore = 0.75;
}

elseif ( $tornadoball >= 40 ): {
    $tornadoScore = 1.00;
}

else: {
    echo " Value";
}

endif;

$tornadoVal == floatval($tornadoScore);

echo $tornadoVal;

6 个答案:

答案 0 :(得分:3)

也许你的意思是>= min<= max,(介于两者之间):

$tornadoball = 22;
$tornadoScore = 0;

if($tornadoball >= 20 && $tornadoball <= 29) { // if this number is in between 20 to 29
    $tornadoScore = 0.25;
}
elseif($tornadoball >= 30 && $tornadoball <= 34) { // if this number is in between 30 to 34
    $tornadoScore = 0.50;
}
elseif($tornadoball >= 35 && $tornadoball <= 39 ) { // if this number is in between 35 to 39
    $tornadoScore = 0.75;
}
elseif($tornadoball >= 40 ) { // if this number is greater than 40
    $tornadoScore = 1.00;
}
else {
    echo 'did not meet any criteria';
}

var_dump($tornadoScore);

答案 1 :(得分:1)

您无需撰写:endif;

这应该有效:

<?php
    $tornadoball = 22;
    $tornadoVal = "";
    $tornadoScore = "";

    if ( $tornadoball <= 20 && $tornadoball >= 29) 
    {
      $tornadoScore = 0.25; 
    } 
    elseif ( $tornadoball <= 30 && $tornadoball >= 34) 
    {
        $tornadoScore = 0.50;

    } 
    elseif ( $tornadoball <= 35 && $tornadoball >= 39 ) 
    {
        $tornadoScore = 0.75;

    } 
    elseif ( $tornadoball >= 40 ) 
    {
        $tornadoScore = 1.00;
    } 
    else
    {
        echo " Value";
    }

    $tornadoVal == floatval($tornadoScore);

    echo $tornadoVal;

?>

答案 2 :(得分:1)

更改此行代码

if ( $tornadoball <= 20 && $tornadoball >= 29):

到这个

 if ( $tornadoball <= 20 && $tornadoball >= 29)

删除冒号 并删除

endif;

答案 3 :(得分:1)

尝试这个并**阅读答案中的评论

第二 您的大多数情况永远不会得到满足。所以我改变了看看

<?php $tornadoball = 22;
$tornadoVal = "";
$tornadoScore = "";
//Below if condition is wrong
if ( $tornadoball >= 20 && $tornadoball <= 29): {
$tornadoScore = 0.25;
}

elseif ( $tornadoball >= 30 && $tornadoball <= 34): {
$tornadoScore = 0.50;
}

elseif ( $tornadoball >= 35 && $tornadoball <= 39 ): {
$tornadoScore = 0.75;
}

elseif ( $tornadoball >= 40 ): {
$tornadoScore = 1.00;
}

else: {
echo " Value";
}

endif;

$tornadoVal = floatval($tornadoScore);
//You got two equal, two equal are comparistion operator
//use one equal. Thats assignment operator

echo $tornadoVal;

答案 4 :(得分:1)

您好请检查下面的代码..这就是您期望输出的内容..

$tornadoball = 22;
$tornadoVal = "";
$tornadoScore = "";

if (($tornadoball <= 20) || ($tornadoball >= 29)) 
{
    $tornadoScore = 0.25;
}

else if (($tornadoball <= 30) ||($tornadoball >= 34)) 
{
    $tornadoScore = 0.50;
}

else if (($tornadoball <= 35) || ($tornadoball >= 39)) 
{
    $tornadoScore = 0.75;
}

else if ($tornadoball >= 40 )
{
    $tornadoScore = 1.00;
}

else 
{
    $tornadoScore= " Value";
}


$tornadoVal = floatval($tornadoScore);

echo $tornadoScore;

答案 5 :(得分:1)

<?php

// initialization
$tornadoball = 22;
$tornadoVal = "";
$tornadoScore = "";

//Below if condition is wrong
if ( $tornadoball >= 20 && $tornadoball <= 29) {
$tornadoScore = 0.25;
}

elseif ( $tornadoball <= 30 && $tornadoball >= 34) {
$tornadoScore = 0.50;
}

elseif ( $tornadoball <= 35 && $tornadoball >= 39 ) {
$tornadoScore = 0.75;
}

elseif ( $tornadoball >= 40 ) {
$tornadoScore = 1.00;
}
else {
// do nothing
}

echo "Score Value = " . $tornadoScore;

$tornadoVal = floatval($tornadoScore);
//You got two equal, two equal are comparistion operator
//use one equal. Thats assignment operator

echo "<br>";
echo "Tornado Value = " . $tornadoVal;
?>