Skip to content

Instantly share code, notes, and snippets.

@IhwanID
Created August 2, 2018 22:45
Show Gist options
  • Save IhwanID/03df8f8587d9bdddceb2ee0c44401a18 to your computer and use it in GitHub Desktop.
Save IhwanID/03df8f8587d9bdddceb2ee0c44401a18 to your computer and use it in GitHub Desktop.
Sample code of if else, switch case, while, do while, for and foreach
<?php
$umur = 17;
// if else statement
if ($umur < 16){
echo "Wah Masih muda ya!";
}elseif ($umur = 17) {
echo "Umur kamu 17, yang semangat belajarnya";
} else {
echo "Udah tua nih bang, Harus lebih rajin dong!";
}
echo "</br></br>";
$warna = "merah";
//switch case statement
switch ($warna) {
case "biru":
echo "Warnanya biru nih!";
break;
case "kuning":
echo "Kuning kayak pisang aja!";
break;
case "merah":
echo "merah itu berani bro!";
break;
default:
echo "tulis yang bener bang!";
}
echo "</br></br>";
$x = 3;
// while statement
while($x <= 5) {
echo "Angka nya adalah $x <br>";
$x++;
}
echo "</br></br>";
//do while statement
do {
echo "Nomor $x <br>";
$x++;
} while ($x <= 5);
echo "</br></br>";
//for statement
for ($i = 0; $i <= 10; $i++) {
echo "Hitungan ke $i <br>";
}
echo "</br></br>";
$majalengka = array("bupati", "pejabat", "guru", "dosen");
//foreach statement
foreach ($majalengka as $isi) {
echo "<li>$isi</li>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment