Learning Basic Logic In PHP
Exercise 1
Display the letter grade based on the Nilai (Score) variable. The conditions we will use are as shown in the table below.
- 85-100: A
- 75-84: B
- 60-74: C
- 50-59: D
- 0-49: E
The following code is the PHP implementation of the problem above:
<?php
$nilai = (float) $_POST["q"];
if ($nilai > 100) {
$rank = "???";
} elseif ($nilai >= 85) {
$rank = "A";
} elseif ($nilai >= 75) {
$rank = "B";
} elseif ($nilai >= 60) {
$rank = "C";
} elseif ($nilai >= 50) {
$rank = "D";
} elseif ($nilai < 50) {
$rank = "E";
}
?>
Program Objective: To determine and classify a letter grade based on numeric score input according to a predefined range.
Input Retrieval: The code receives input via the HTTP POST method with the parameter “q”, which is then converted into a decimal data type (float) and stored in the $nilai variable.
Logical Structure (If-Elseif):
-
The program uses sequential branching to match the score range with the guideline table:
-
Invalid Value Handling: If
$nilaiis more than 100, the$rankvariable is assigned the value “???”. -
Grade A: If
$nilaiis greater than or equal to 85 (representing the 85-100 range),$rankbecomes “A”. -
Grade B: If
$nilaiis greater than or equal to 75 (representing the 75-84 range),$rankbecomes “B”. -
Grade C: If
$nilaiis greater than or equal to 60 (representing the 60-74 range),$rankbecomes “C”. -
Grade D: If
$nilaiis greater than or equal to 50 (representing the 50-59 range),$rankbecomes “D”. -
Grade E: If
$nilaiis less than 50 (representing the 0-49 range),$rankbecomes “E”.
Output: The final result of this branching process is the assignment of a value to the $rank variable, which is ready to be displayed to the user.
Exercise 2
Display the IQ Percentage of the world population based on the table below.
- 0-29 = Idiot
- 30-40 = Mental Retardation
- 50-69 = Very Low IQ
- 70-79 = Moron
- 80-89 = Slow
- 90-109 = Average
- 110-119 = Normal Bright
- 120-129 = Smart
- 130-139 = Very Smart (Superior)
- 140+ = Genius
The following code is the PHP implementation of the problem above:
<?php
$iq = (float) $_POST["q"];
if ($iq >= 140) {
$keterangan = "Genius";
} elseif ($iq >= 130) {
$keterangan = "Very Smart (Superior)";
} elseif ($iq >= 120) {
$keterangan = "Smart";
} elseif ($iq >= 110) {
$keterangan = "Normal Bright";
} elseif ($iq >= 90) {
$keterangan = "Average";
} elseif ($iq >= 80) {
$keterangan = "Slow";
} elseif ($iq >= 70) {
$keterangan = "Moron";
} elseif ($iq >= 50) {
$keterangan = "Very Low IQ";
} elseif ($iq >= 30) {
$keterangan = "Mental Retardation";
} elseif ($iq >= 0) {
$keterangan = "Idiot";
} else {
$keterangan = "Invalid value";
}
?>
Program Objective: To determine and display the description for a specific IQ score based on the guideline table of world population IQ percentages.
Input Retrieval: The program receives input values from a form via the POST method using the parameter “q”. This value is then converted into a decimal data type (float) and stored in the $iq variable.
Logical Structure (If-Elseif): The program uses a series of conditional branches from the highest score range to the lowest to align the logic with the table:
-
140+: If
$iqis greater than or equal to 140, the$keteranganvariable is assigned “Genius”. -
130-139: If
$iqis greater than or equal to 130, the$keteranganvariable is assigned “Very Smart (Superior)”. -
120-129: If
$iqis greater than or equal to 120, the$keteranganvariable is assigned “Smart”. -
110-119: If
$iqis greater than or equal to 110, the$keteranganvariable is assigned “Normal Bright”. -
90-109: If
$iqis greater than or equal to 90, the$keteranganvariable is assigned “Average”. -
80-89: If
$iqis greater than or equal to 80, the$keteranganvariable is assigned “Slow”. -
70-79: If
$iqis greater than or equal to 70, the$keteranganvariable is assigned “Moron”. -
50-69: If
$iqis greater than or equal to 50, the$keteranganvariable is assigned “Very Low IQ”. -
30-40: If
$iqis greater than or equal to 30, the$keteranganvariable is assigned “Mental Retardation”. -
0-29: If
$iqis greater than or equal to 0, the$keteranganvariable is assigned “Idiot”.
Invalid Value Handling: If the $iq value does not meet any of the conditions above (e.g., the value is negative or below 0), the program will enter the else block, and the $keterangan variable will be assigned the string “Invalid value”.
Exercise 3
Display the time description based on the current hour.
- Morning (Pagi): 05.01 – 11.00
- Afternoon (Siang): 11.01 – 15.00
- Evening (Sore): 15.01 – 17.00
- Night (Malam): 17.01 – 24.00
- Early Morning (Dini Hari): 00.01 – 05.00
The following code is the PHP implementation of the problem above:
<?php
$jam = (float) date("H.i");
if ($jam > 17.00 && $jam <= 24.00) {
$waktu = "Night";
} elseif ($jam > 15.00 && $jam <= 17.00) {
$waktu = "Evening";
} elseif ($jam > 11.00 && $jam <= 15.00) {
$waktu = "Afternoon";
} elseif ($jam > 5.00 && $jam <= 11.00) {
$waktu = "Morning";
} elseif ($jam >= 0.00 && $jam <= 5.00) {
$waktu = "Early Morning";
} else {
$waktu = "Invalid time";
}
?>
Program Objective: To determine and display the time division description (such as Morning, Afternoon, Evening) based on the current system time according to the provided time range guidelines.
Input Retrieval: The program retrieves the current system time using the built-in PHP function date("H.i"), which produces a time format of hours and minutes (HH.MM). This time value is then converted into a decimal data type (float) and stored in the $jam variable to facilitate comparison.
Logical Structure (If-Elseif): The program uses a conditional branching structure (with the logical AND operator &&) to map the $jam variable value according to the Exercise 3 guidelines:
-
Night (Malam): If
$jamis greater than 17.00 and less than or equal to 24.00 (representing the 17.01-24.00 range), the$waktuvariable is assigned “Night”. -
Evening (Sore): If
$jamis greater than 15.00 and less than or equal to 17.00 (representing the 15.01-17.00 range), the$waktuvariable is assigned “Evening”. -
Afternoon (Siang): If
$jamis greater than 11.00 and less than or equal to 15.00 (representing the 11.01-15.00 range), the$waktuvariable is assigned “Afternoon”. -
Morning (Pagi): If
$jamis greater than 5.00 and less than or equal to 11.00 (representing the 05.01-11.00 range), the$waktuvariable is assigned “Morning”. -
Early Morning (Dini Hari): If
$jamis greater than or equal to 0.00 and less than or equal to 5.00 (representing the 00.01-05.00 range), the$waktuvariable is assigned “Early Morning”.
Invalid Value Handling: If for some reason the $jam value falls outside the 0.00 to 24.00 range, the program will automatically execute the else block, and the $waktu variable will be assigned the string “Invalid time”.