一次运行多个if语句PHP

时间:2017-04-27 09:15:40

标签: php if-statement

我正在尝试将从表格中选择的数量打印到另一页面。我有一系列if语句,用于显示是否已输入数量,如果有,则显示数量和项目。

问题是它只运行第一个if语句并忽略其余的语句。如果有人知道如何运行它们,那将是一个很大的帮助!

<?php

  if(!empty($_POST['qtybang']))
  {
    echo $qtybang.' Bang Bang Chicken<br>';
  }

  if(!empty($_POST['$qtyFlatbread']))
  {
    echo $qtyFlatbread.' Flatbread<br>';
  }

  if(!empty($_POST['$qtyMexican']))
  {
  echo $qtyMexican.' Mexican Bean Stew<br>';
  }

  if(!empty($_POST['$qtyrice']))
  {
  echo $qtyrice.' Rice Bowl<br>';
  }

  if(!empty($_POST['$qtySandwiche']))
  {
  echo $qtySandwiche.' sandwiches<br>';
  }


  if(!empty($_POST['$qtyCapachino']))
  {
  echo $qtyCapachino.' Capachino<br>';
  }


  if(!empty($_POST['$qtyAmericano']))
  {      
  echo $qtyAmericano.' Americano<br>';
  }


  if(!empty($_POST['$qtyExpresso']))
  {
  echo $qtyExpresso.' Expresso<br>';
  }?>

Order Table

Output

1 个答案:

答案 0 :(得分:1)

实际处理了if语句,但只是检查过的vars为空,这就是为什么你没有输出。 这是因为$ _POST列表中的$在除第一个条件之外的所有实例上都是错误的。

    <?php
     $_POST['qtybang'] = 'something';
     $qtybang = $_POST['qtybang'];
     $_POST['$qtyFlatbread'] = 'something';
     $qtyFlatbread = $_POST['$qtyFlatbread'];

     if(!empty($_POST['qtybang']))
     {
       echo $qtybang.' Bang Bang Chicken<br>'."\n";
     }

     if(!empty($_POST['$qtyFlatbread']))
     {
       echo $qtyFlatbread.' Flatbread<br>'."\n";
     }

     if(!empty($_POST['$qtyMexican']))
     {
       echo $qtyMexican.' Mexican Bean Stew<br>'."\n";
     }

     if(!empty($_POST['$qtyrice']))
     {
       echo $qtyrice.' Rice Bowl<br>'."\n";
     }

     if(!empty($_POST['$qtySandwiche']))
     {
       echo $qtySandwiche.' sandwiches<br>'."\n";
     }


     if(!empty($_POST['$qtyCapachino']))
     {
      echo $qtyCapachino.' Capachino<br>'."\n";
     }


     if(!empty($_POST['$qtyAmericano']))
     {      
       echo $qtyAmericano.' Americano<br>'."\n";
     }


     if(!empty($_POST['$qtyExpresso']))
     {
       echo $qtyExpresso.' Expresso<br>'."\n";
     }
?>