Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.

Pages

Learning PHP: Using Branching to Create Program Logic

Structure conditions in PHP
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:
Branchless flow chart program
If we want to add branching, we will make it like this:
Branching flow chart
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:
Example condition if
Consider the example above!
Text Anda dapat hadiah!will only be displayed when the condition of the variable $total_belanjais above 100000If below 100000, nothing will show.
The conditions used in the example are:
$total_belanja > 100000
This condition or statement will be valuable trueand falseIf true(correct), then the code in it will be executed. However, if falseit 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:
Example of an if / else condition
Now try changing the value $umurto 19:
<?php

$umur = 19;

if ($umur < 18 ){
    echo "Kamu tidak boleh membuka situs ini!";
} else {
    echo "Selamat datang di website kami!";
}
?>
The result:
An example of an if / else condition is correct

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:
Example of if / elseif / else condition
Try changing variables $nilaito 54and 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 defaultwill be selected if the variable value is $levelnot in the selection case.
The result:
Examples of switch / case conditions
Try changing the value of the variable $leveland 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 $sukavaluable truethen print "Aku juga suka kamu"But if it's worth false, then print "Baiklah!".
The result:
Examples of conditions with ternary operators

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:
Examples of nesting conditions


Reference: https://www.petanikode.com/php-percabangan/
0 Komentar untuk "Learning PHP: Using Branching to Create Program Logic"

Silahkan berkomentar sesuai artikel

 
Template By Kunci Dunia
Back To Top