为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
至尊宝的传说 2022-06-11 09:59:35
我有一个关于使用 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>
查看完整描述

2 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

句法


if (condition) {

    code to be executed if this condition is true;

} elseif (condition) {

    code to be executed if first condition is false and this condition is true;

} else {

    code to be executed if all conditions are false;


最后试试这个,否则不需要添加条件


<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";

     }


    }

   }

   else {

    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>


查看完整回答
反对 回复 2022-06-11
?
当年话下

TA贡献1890条经验 获得超9个赞

您似乎正在寻找的是$hasil具有最大值的条目的键。有很多方法可以做到这一点,但我建议对数组进行排序,同时保留键,然后在键值上使用开关。像这样:


<div class="alert alert-info">

  <?php  

   arsort($hasil);

   switch(array_key_first($hasil)) {

     case 0  : $hasilkonsentrasi = "Manajemen Keamanan Jaringan";

               break;   

     case 1  : $hasilkonsentrasi = "Teknologi Cerdas";

               break;   

     case 2  : $hasilkonsentrasi = "Manajemen Bisnis";

               break;   

     case 3  : $hasilkonsentrasi = "Manajemen Data dan Informasi";

               break;   

     default : $hasilkonsentrasi = "Not found";

               break;   

   }

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

  ?>

</div>  

当数组中有 6 个条目时,这仍然是可行的,而如果你想用if () else代码来做这件事,真的会变得不可读。


查看完整回答
反对 回复 2022-06-11
  • 2 回答
  • 0 关注
  • 86 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信