If you are an Informatics Engineering student, you will definitely not be familiar with branching.
Branching is the term for a branched program flow.
In flowcharts, we often draw program flow like this:
If we want to add branching, we will make it like this:
On this occasion, we will learn about branching to completion and make a few sample programs.
If branching
The simplest form of conversation is "If" only.
Usually used when there is only one action that must be done.
Form it like this:
<?php
if (<kondisi>){
// eksekusi kode ini
}
If the condition is correct, then execute the code in it.
<kondisi>
we can fill with a boolean value or we foam create a statement to produce a booleanvalue .
Example:
<?php
$total_belanja = 150000;
if($total_belanja > 100000){
echo "Anda dapat hadiah!";
}
The result:
Consider the example above!
Text
Anda dapat hadiah!
will only be displayed when the condition of the variable $total_belanja
is above 100000
. If below 100000
, nothing will show.
The conditions used in the example are:
$total_belanja > 100000
This condition or statement will be valuable
true
and false
. If true
(correct), then the code in it will be executed. However, if false
it does not execute it.If / Else Branching
If / Else branching has two choices. If it is
<kondisi>
worth false
, then another block will be done.
Example:
<?php
$umur = 13;
if ($umur < 18 ){
echo "Kamu tidak boleh membuka situs ini!";
} else {
echo "Selamat datang di website kami!";
}
?>
The result:
Now try changing the value
$umur
to 19
:<?php
$umur = 19;
if ($umur < 18 ){
echo "Kamu tidak boleh membuka situs ini!";
} else {
echo "Selamat datang di website kami!";
}
?>
The result:
If / Elseif / Else Branching
If / Elseif / Else branching has more than two conditions.
Example:
<?php
$nilai = 88;
if ($nilai > 90) {
$grade = "A+";
} elseif($nilai > 80){
$grade = "A";
} elseif($nilai > 70){
$grade = "B+";
} elseif($nilai > 60){
$grade = "B";
} elseif($nilai > 50){
$grade = "C+";
} elseif($nilai > 40){
$grade = "C";
} elseif($nilai > 30){
$grade = "D";
} elseif($nilai > 20){
$grade = "E";
} else {
$grade = "F";
}
echo "Nilai anda: $nilai<br>";
echo "Grade: $grade";
?>
The result:
Try changing variables
$nilai
to 54
and pay attention to the results!Branching / Case Branching
Branching / Case branching is another form of If / Elseif / Else branching.
The writing format is like this:
<?php
switch($variabel){
case <konidisi>:
// eksekusi kode ini
break;
case <kondisi2>:
// eksekusi kode ini
break;
default:
// eksekusi kode ini
}
?>
Example:
<?php
$level = 3;
switch($level){
case 1:
echo "Pelajari HTML";
break;
case 2:
echo "Pelajari CSS";
break;
case 3:
echo "Pelajari Javascript";
break;
case 4:
echo "Pelajari PHP";
break;
default:
echo "Kamu bukan programmer!";
}
?>
There are 5 choices under the conditions above. Options
default
will be selected if the variable value is $level
not in the selection case
.
The result:
Try changing the value of the variable
$level
and pay attention to the results.Branching with Ternary Operators
Branching using the ternary operator is a simple form of If / Else branching.
Format it like this:
<?php
<kondisi> ? benar : salah;
?>
Example:
<?php
$suka = true;
$suka ? echo "Aku juga suka kamu": echo "Baiklah!";
?>
Or it can also be made like this:
<?php
$suka = true;
echo $suka ? "Aku juga suka kamu": "Baiklah!";
?>
Meaning: if the variable is
$suka
valuable true
then print "Aku juga suka kamu"
. But if it's worth false
, then print "Baiklah!"
.
The result:
Nesting Branches
Nesting branching means there is a branching in nested .
Example:
<?php
$umur = 17;
$menikah = false;
if($umur > 18){
if($menikah){
echo "Selamat datang pak!";
} else {
echo "Selamat datang wahai pemuda!";
}
} else {
echo "Maaf website ini hanya untuk yang sudah berumur 18+";
}
?>
The result:
Reference: https://www.petanikode.com/php-percabangan/
0 Komentar untuk "Learning PHP: Using Branching to Create Program Logic"
Silahkan berkomentar sesuai artikel