KrnlProjekt

collection of my things like work, or something something...

established august 2026

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 $nilai is more than 100, the $rank variable is assigned the value “???”.

  • Grade A: If $nilai is greater than or equal to 85 (representing the 85-100 range), $rank becomes “A”.

  • Grade B: If $nilai is greater than or equal to 75 (representing the 75-84 range), $rank becomes “B”.

  • Grade C: If $nilai is greater than or equal to 60 (representing the 60-74 range), $rank becomes “C”.

  • Grade D: If $nilai is greater than or equal to 50 (representing the 50-59 range), $rank becomes “D”.

  • Grade E: If $nilai is less than 50 (representing the 0-49 range), $rank becomes “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 $iq is greater than or equal to 140, the $keterangan variable is assigned “Genius”.

  • 130-139: If $iq is greater than or equal to 130, the $keterangan variable is assigned “Very Smart (Superior)”.

  • 120-129: If $iq is greater than or equal to 120, the $keterangan variable is assigned “Smart”.

  • 110-119: If $iq is greater than or equal to 110, the $keterangan variable is assigned “Normal Bright”.

  • 90-109: If $iq is greater than or equal to 90, the $keterangan variable is assigned “Average”.

  • 80-89: If $iq is greater than or equal to 80, the $keterangan variable is assigned “Slow”.

  • 70-79: If $iq is greater than or equal to 70, the $keterangan variable is assigned “Moron”.

  • 50-69: If $iq is greater than or equal to 50, the $keterangan variable is assigned “Very Low IQ”.

  • 30-40: If $iq is greater than or equal to 30, the $keterangan variable is assigned “Mental Retardation”.

  • 0-29: If $iq is greater than or equal to 0, the $keterangan variable 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 $jam is greater than 17.00 and less than or equal to 24.00 (representing the 17.01-24.00 range), the $waktu variable is assigned “Night”.

  • Evening (Sore): If $jam is greater than 15.00 and less than or equal to 17.00 (representing the 15.01-17.00 range), the $waktu variable is assigned “Evening”.

  • Afternoon (Siang): If $jam is greater than 11.00 and less than or equal to 15.00 (representing the 11.01-15.00 range), the $waktu variable is assigned “Afternoon”.

  • Morning (Pagi): If $jam is greater than 5.00 and less than or equal to 11.00 (representing the 05.01-11.00 range), the $waktu variable is assigned “Morning”.

  • Early Morning (Dini Hari): If $jam is greater than or equal to 0.00 and less than or equal to 5.00 (representing the 00.01-05.00 range), the $waktu variable 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”.