if / else if语句的结构和php运算符混淆

时间:2018-11-02 03:08:38

标签: php

我不想问这样一个单调乏味的问题,但是我在构建我的if else陈述时遇到了麻烦。我知道它无法正常执行,因为它只会给我第一个回音作为答案。我不太了解php中的&&运算符,几天前我才刚开始使用这种语言。我觉得我的代码应该可以工作,但是绝对不能,所以我搞砸了。

如果我在get方法之前尝试isset,则会收到一条错误消息,提示您使用null。

我尝试将运算符更改为||但这并不是我要在语句中查找的内容。我也尝试过!=,但我可能在构造语法错误。

    <?php
        $power = "";

        if (isset($_GET['power']) < '0.0') 
        {
          echo "Po eats too much noodle";
        } 
        else if (isset($_GET['power']) <= '20.0') 
        {
          echo "Po reaches the training hall";
        } 
        else if (isset($_GET['power']) >= '21.0' && isset($_GET['power']) <= '40.0') 
        {
          echo "Po reaches the student barracks";
        } 
        else if (isset($_GET['power']) >= '41.0' && isset($_GET['power']) <= '60.0') 
        {
          echo "Po reaches the Peach Tree of Heavenly Wisdom";
        } 
        else if (isset($_GET['power']) >= '61.0' && isset($_GET['power']) <= '80.0') 
        {
          echo "Po climbs the Wu Da Mountains";
        } 
        else if (isset($_GET['power']) >= '81.0' && isset($_GET['power']) <= '100.0') 
        {
          echo "Po trains at the Pool of Sacred Tears";
        } 
        else 
        {
          echo "Po defeats Tai Lung";
        } 
     ?>

感谢所有提供帮助的人!

<?php
    $power = "";

    if ($power = ($_GET['power'] < 0.0)) 
    {
      echo "Po eats too much noodle";
    } 
    else if ($power = $_GET['power'] <= 20.0) 
    {
      echo "Po reaches the training hall";
    } 
    else if (($power = $_GET['power'] >= 21.0) && ($power = $_GET['power'] <= 40.0)) 
    {
      echo "Po reaches the student barracks";
    } 
    else if (($power = $_GET['power'] >= 41.0) == ($power = $_GET['power'] <= 60.0))
    {
      echo "Po reaches the Peach Tree of Heavenly Wisdom";
    } 
    else if (($power = $_GET['power'] >= 61.0) || ($power = $_GET['power'] <= 80.0)) 
    {
      echo "Po climbs the Wu Da Mountains";
    } 
    else if (($power = $_GET['power'] > 80.0) && ($power = $_GET['power'] < 100.0)) 
    {
      echo "Po trains at the Pool of Sacred Tears";
    } 
    else 
    {
      echo "Po defeats Tai Lung";
    } 
 ?>

这是我更新的内容。谢谢:)我希望我正在做结构正确吗?我现在可以通过它,但是它不会执行最后两部分。以某种方式与操作员打交道虽然有所帮助,但也没有帮助。

2 个答案:

答案 0 :(得分:1)

首先,您需要了解代码重构。您可以简单地将$_GET['power']提取到变量中并使用它。

在PHP中回答问题&&与在大多数编程语言(如Java / C)中的&&类似。它只是用来检查两个条件是否都为真。

我在您的代码中看到另一个问题,看起来$power是一个浮动值。所以我将其转换为字符串的浮点数。另外,当您进行比较时,会丢失条件中的诸如20.5、40.5 ..等值。签出共享代码。

<?php
$power = floatval($_GET['power']);

if ($power < 0.0) {
    echo "Po eats too much noodle";
} else if ($power <= 20.0) {
    echo "Po reaches the training hall";
} else if ($power <= 40.0) {
    echo "Po reaches the student barracks";
} else if ($power <= 60.0) {
    echo "Po reaches the Peach Tree of Heavenly Wisdom";
} else if ($power <= 80.0) {
    echo "Po climbs the Wu Da Mountains";
} else if ($power < 100.0) {
    echo "Po trains at the Pool of Sacred Tears";
} else {
    echo "Po defeats Tai Lung";
}
?>

答案 1 :(得分:0)

此代码将起作用

<?php
    $power = $_GET['power'];
    if ($power < 0.0) 
    {
      echo "Po eats too much noodle";
    } 
    else if($power <= 20.0) 
    {
      echo "Po reaches the training hall";
    } 
    else if (($power >= 21.0) && ($power <= 40.0)) 
    {
      echo "Po reaches the student barracks";
    } 
    else if (($power >= 41.0) && ($power <= 60.0)) 
    {
      echo "Po reaches the Peach Tree of Heavenly Wisdom";
    } 
    else if (($power >= 61.0) && ($power <= 80.0)) 
    {
      echo "Po climbs the Wu Da Mountains";
    } 
    else if (($power >= 81.0) && ($power <= 100.0)) 
    {
      echo "Po trains at the Pool of Sacred Tears";
    } 
    else 
    {
      echo "Po defeats Tai Lung";
    } 
 ?>