martinms

7 Operator Logika

Jul 23rd, 2021
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. <?php
  2.  
  3. function apakah_prodi_ft($kode_prodi = null)
  4. {
  5.   $daftar_prodi = array(
  6.     'A' => 'Informatika',
  7.     'B' => 'Teknik Sipil',
  8.     'C' => 'Teknik Mesin',
  9.     'D' => 'Teknik Elektro',
  10.     'E' => 'Arsitektur',
  11.     'F' => 'Sistem Informasi',
  12.   );
  13.  
  14.   // linear search
  15.   foreach ($daftar_prodi as $kode => $prodi) {
  16.     if ($kode == $kode_prodi) {
  17.       // jika $kode_prodi ada di dalam array $daftar_prodi, maka hentikan looping
  18.       // dan kembalikan nilai fungsi "true"
  19.       return true;
  20.       break;
  21.     }
  22.   }
  23.  
  24.   // jika $kode_prodi tidak ada di dalam array $daftar_prodi,
  25.   // kembalikan nilai fungsi "false"
  26.   return false;
  27. }
  28.  
  29. // Data pendaftar
  30. $prodi = 'A';
  31. $semester = 5;
  32. $ip = 3;
  33.  
  34. if (apakah_prodi_ft($prodi) && $ip >= 3) { // operator and. pendaftar harus mahasiswa ft dan ip lebih besar atau sama dengan 3
  35.   echo 'Anda mahasiswa Fakultas teknik dengan IP >= 3. Boleh mendaftar';
  36. } else {
  37.   echo 'Anda bukan mahasiswa Fakultas teknik atau IP tidak memenuhi syarat. Tidak boleh mendaftar';
  38. }
  39.  
  40. echo '<br>';
  41. echo '<br>';
  42.  
  43. var_dump ( ! true); // negasi (membalik nilai)
  44. ?>
  45.  
  46.  
  47. <table border="1">
  48.   <thead>
  49.     <th>p</th>
  50.     <th>q</th>
  51.     <th>~ p</th>
  52.     <th>~ q</th>
  53.     <th>p ^ q</th>
  54.     <th>~p ^ ~q</th>
  55.   </thead>
  56.   <tbody>
  57.     <tr>
  58.       <td>
  59.         <?php $p = true; ?>
  60.         true
  61.       </td>
  62.       <td>
  63.         <?php $q = false; ?>
  64.         false
  65.       </td>
  66.       <td><?php var_dump( ! $p); ?></td>
  67.       <td><?php var_dump( ! $q); ?></td>
  68.       <td><?php var_dump($p && $q); ?></td>
  69.       <td><?php var_dump( ! $p && ! $q); ?></td>
  70.     </tr>
  71.    
  72.   </tbody>
  73. </table>
Advertisement
Add Comment
Please, Sign In to add comment