如果嵌套在 PHP 中,如何使用?

我有一个关于使用 if else 嵌套在 php 中的问题,这$hasilkonsentrasi是不可读的,谢谢


<div class="alert alert-info">

  <?php  

   if ($hasil[0] > $hasil[1]) {

    if ($hasil[0] > $hasil[2]) {

     if ($hasil[0] > $hasil[3]) {

     $hasilkonsentrasi = "Manajemen Keamanan Jaringan";

     }     

    }

   }

   elseif ($hasil[1] > $hasil[0]) {

    if ($hasil[1] > $hasil[2]) {

     if ($hasil[1] > $hasil[3]) {

      $hasilkonsentrasi = "Teknologi Cerdas";

     }

    }

   }

   elseif ($hasil[2] > $hasil[0]) {

    if ($hasil[2] > $hasil[1]) {

     if ($hasil[2] > $hasil [3]) {

      $hasilkonsentrasi = "Manajemen Bisnis";

     }


    }

   }

   elseif ($hasil[3] > $hasil[0]) {

    if ($hasil[3] > $hasil[1]) {

     if ($hasil[3] > $hasil[2]) {

      $hasilkonsentrasi = "Manajemen Data dan Informasi";

     }


    }

   }

   echo "Anda cocok mengambil konsentrasi <strong>$hasilkonsentrasi</strong>";



  $mkj= number_format($hasil[0], 2, '.', '');

  $tc=$hasil[1];

  $mb=$hasil[2];

  $mdi=$hasil[3];

  $jurusan=$hasilkonsentrasi;

  $user->SimpanHasilJurusan($mkj,$tc,$mb,$mdi,$jurusan);

  ?>

 </div>

</div>


至尊宝的传说
浏览 96回答 2
2回答

BIG阳

句法if (condition) {&nbsp; &nbsp; code to be executed if this condition is true;} elseif (condition) {&nbsp; &nbsp; code to be executed if first condition is false and this condition is true;} else {&nbsp; &nbsp; code to be executed if all conditions are false;}&nbsp;最后试试这个,否则不需要添加条件<div class="alert alert-info">&nbsp; <?php&nbsp;&nbsp;&nbsp; &nbsp;if ($hasil[0] > $hasil[1]) {&nbsp; &nbsp; if ($hasil[0] > $hasil[2]) {&nbsp; &nbsp; &nbsp;if ($hasil[0] > $hasil[3]) {&nbsp; &nbsp; &nbsp;$hasilkonsentrasi = "Manajemen Keamanan Jaringan";&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;elseif ($hasil[1] > $hasil[0]) {&nbsp; &nbsp; if ($hasil[1] > $hasil[2]) {&nbsp; &nbsp; &nbsp;if ($hasil[1] > $hasil[3]) {&nbsp; &nbsp; &nbsp; $hasilkonsentrasi = "Teknologi Cerdas";&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;elseif ($hasil[2] > $hasil[0]) {&nbsp; &nbsp; if ($hasil[2] > $hasil[1]) {&nbsp; &nbsp; &nbsp;if ($hasil[2] > $hasil [3]) {&nbsp; &nbsp; &nbsp; $hasilkonsentrasi = "Manajemen Bisnis";&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;else {&nbsp; &nbsp; if ($hasil[3] > $hasil[1]) {&nbsp; &nbsp; &nbsp;if ($hasil[3] > $hasil[2]) {&nbsp; &nbsp; &nbsp; $hasilkonsentrasi = "Manajemen Data dan Informasi";&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;echo "Anda cocok mengambil konsentrasi <strong>$hasilkonsentrasi</strong>";&nbsp; $mkj= number_format($hasil[0], 2, '.', '');&nbsp; $tc=$hasil[1];&nbsp; $mb=$hasil[2];&nbsp; $mdi=$hasil[3];&nbsp; $jurusan=$hasilkonsentrasi;&nbsp; $user->SimpanHasilJurusan($mkj,$tc,$mb,$mdi,$jurusan);&nbsp; ?>&nbsp;</div></div>

当年话下

您似乎正在寻找的是$hasil具有最大值的条目的键。有很多方法可以做到这一点,但我建议对数组进行排序,同时保留键,然后在键值上使用开关。像这样:<div class="alert alert-info">&nbsp; <?php&nbsp;&nbsp;&nbsp; &nbsp;arsort($hasil);&nbsp; &nbsp;switch(array_key_first($hasil)) {&nbsp; &nbsp; &nbsp;case 0&nbsp; : $hasilkonsentrasi = "Manajemen Keamanan Jaringan";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;case 1&nbsp; : $hasilkonsentrasi = "Teknologi Cerdas";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;case 2&nbsp; : $hasilkonsentrasi = "Manajemen Bisnis";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;case 3&nbsp; : $hasilkonsentrasi = "Manajemen Data dan Informasi";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;default : $hasilkonsentrasi = "Not found";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp;&nbsp; &nbsp;}&nbsp; &nbsp;echo "Anda cocok mengambil konsentrasi <strong>$hasilkonsentrasi</strong>";&nbsp; ?></div>&nbsp;&nbsp;当数组中有 6 个条目时,这仍然是可行的,而如果你想用if () else代码来做这件事,真的会变得不可读。
打开App,查看更多内容
随时随地看视频慕课网APP