解析错误:语法错误,意外的“ elseif”,多个elseif

时间:2018-06-19 15:17:32

标签: php if-statement syntax-error

这是我的代码:

<?php
   if ($data[0][extraUrbanKmConsumption]<85 AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/A.png" class="img-fluid" alt="Responsive image"></h5>
    <?php } ?> 
  <?php elseif ($data[0][extraUrbanKmConsumption]>=85 AND $data[0][extraUrbanKmConsumption]<115  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/B.png" class="img-fluid" alt="Responsive image"></h5>
    <?php } ?> 
  <?php elseif ($data[0][extraUrbanKmConsumption]>=115 AND $data[0][extraUrbanKmConsumption]<145  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/C.png" class="img-fluid" alt="Responsive image"></h5>
    <?php } ?> 
  <?php elseif ($data[0][extraUrbanKmConsumption]>=145 AND $data[0][extraUrbanKmConsumption]<175  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/D.png" class="img-fluid" alt="Responsive image"></h5>
    <?php } ?> 
  <?php elseif ($data[0][extraUrbanKmConsumption]>=175 AND $data[0][extraUrbanKmConsumption]<205  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/E.png" class="img-fluid" alt="Responsive image"></h5>
    <?php } ?> 
  <?php elseif ($data[0][extraUrbanKmConsumption]>=205 AND $data[0][extraUrbanKmConsumption]<235  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/F.png" class="img-fluid" alt="Responsive image"></h5>
    <?php } ?> 
  <?php elseif ($data[0][extraUrbanKmConsumption]>=235  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/G.png" class="img-fluid" alt="Responsive image"></h5>
    <?php } ?>

返回此错误的消息:解析错误:语法错误,意外的'elseif'(T_ELSEIF),期望文件结尾

我的语法错误吗?

1 个答案:

答案 0 :(得分:3)

问题在于,您不会在elseifelse之前破坏PHP解析器。这类似于它们之间有一个echo,这使其成为语法错误。使用这种方式:

<?php
  if ($data[0][extraUrbanKmConsumption]<85 AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/A.png" class="img-fluid" alt="Responsive image"></h5>
  <?php } elseif ($data[0][extraUrbanKmConsumption]>=85 AND $data[0][extraUrbanKmConsumption]<115  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/B.png" class="img-fluid" alt="Responsive image"></h5>
  <?php } elseif ($data[0][extraUrbanKmConsumption]>=115 AND $data[0][extraUrbanKmConsumption]<145  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/C.png" class="img-fluid" alt="Responsive image"></h5>
  <?php } elseif ($data[0][extraUrbanKmConsumption]>=145 AND $data[0][extraUrbanKmConsumption]<175  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/D.png" class="img-fluid" alt="Responsive image"></h5>
  <?php } elseif ($data[0][extraUrbanKmConsumption]>=175 AND $data[0][extraUrbanKmConsumption]<205  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/E.png" class="img-fluid" alt="Responsive image"></h5>
  <?php } elseif ($data[0][extraUrbanKmConsumption]>=205 AND $data[0][extraUrbanKmConsumption]<235  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/F.png" class="img-fluid" alt="Responsive image"></h5>
  <?php } elseif ($data[0][extraUrbanKmConsumption]>=235  AND $data[0][energy][name] == "Diesel") { ?>
    <h5 class="text-light">Emission de cO2 : <?php echo $data[0][extraUrbanKmConsumption]; ?> <img src="icone c02/G.png" class="img-fluid" alt="Responsive image"></h5>
  <?php } ?>