Explain Conditional Structure in PHP

Explain conditional structure in php - The structure of conditions owned by PHP is almost the same as other programming languages. A condition structure that is a flow regulator of the program, a series of commands that must be written to meet several conditions, namely:
Condition Structure If
if (condition) {
statement-if-condition-true;
}

Information :
Condition is a statement or variable to be TRUE or FALSE checked.
Structure Condition If ... Else
if (condition) {
statement-if-condition-true;
} else {
statement-if-condition-false;
}

Information :
Condition is a statement or variable to be TRUE or FALSE checked. If the condition is TRUE then the statement in the block if will be executed, otherwise if the condition is FALSE then the statement is on block else to be executed.
Special Condition Structure? :
(condition) ? true : false;
Information :
Condition is a statement or variable to be TRUE or FALSE checked. Statements on true and wrong blocks may only be one statement only
Structure Condition Switch ... Case
switch ($var) {
 case '1' : statement-1; break;
 case '2' : statement-2; break;
....
}

Information :
$var is a variable to be checked for content or value. This variable data type is not restricted.
Value in case can also be a string, integer, boolean, even conditional-statement. May use single quotes or double quotes.
In order to better understand the following I give examples of program application of structure conditions in php.
Explain Conditional Structure in PHP
Program 1
File Name: if.php
Description: Program Structure If Condition.
Open notepad then type the following php code:
<?php
$ value  = 80;
if ($value >= 60) {
 echo " Your score $nilai, You GRADUATED ";
}
?>

Explain Conditional Structure in PHP

Next save the code with the name if.php (in the htdocs folder). To see the result open the browser then type http://localhost/if.php on the address bar. The result will look like the following
Explain Conditional Structure in PHP

Program Explanation 1
In the above program, there is a condition check whether the content of the variable $value more than 60 (line 3). If this condition is TRUE (variable $value contains a value greater than 60) then the statement on line 4 will be executed. Conversely if the condition is FALSE, then statement on line 4 will not be executed.
Program 2
File Name: if_else.php
Description: Program Structure Condition If..Else.
Type the following php code into notepad:
<?php
$value = 50;
if ($value >= 60) {
 echo "Your score $value, You GRADUATED";
} else {
 echo "Your score $value, You FAILED";
}
?>

Explain Conditional Structure in PHP

Save the code with the name if_else.php (in the htdocs folder). To see the result type http: //localhost/if_else.php in the address bar. Then the result will look like the following.
Explain Conditional Structure in PHP

Program Explanation 2
In the above program, there is a condition check whether the content of the variable $value more than 60 (line 3). If this condition is TRUE (variable $value contains a value greater than 60) then the statement on line 4 will be executed. Conversely, if the condition is FALSE, then the statement on line 6 will be executed. In the example of the above program, the condition in line 3 is FALSE because the contents of the variable $value is 50.
Program 3
File Name: if_else2.php
Description: Program Check username and password with If..Else.
Open notepad then type the following php code:
<?php
$user = "johans";
$pass = "123";
if ($user == "johans" && $pass == "123") {
echo "Login successful";
} else {
echo "Login failed";
}
?>

Explain Conditional Structure in PHP

Save the code you input the notepad with the name if_else2.php (in the htdocs folder). If you can see the results by opening the browser, by typing in the address bar http://localhost/if_else2.php. The result will look like in the following picture.
Explain Conditional Structure in PHP

Program Explanation 3
In the above program, there is a condition check whether the contents of the $value variable are equal to "achmatim" and whether the contents of the $pass variable are equal to "123" (line 4). If both conditions are TRUE then the statement on line 5 will be executed. Conversely, if one of them is FALSE, then the statement on line 7 will be executed.
Program 4
File Name: if_var.php
Description: Program Checking a variable exists or not.
Type the following php code into notepad:
<?php
$user="";
if (!isset($user)) {
 echo "Variables are missing / not yet formed";
} else {
 echo "Variable exists";
}
?>

Explain Conditional Structure in PHP

Save the code with the name if_var.php (in the htdocs folder). Then open the browser and type in the address bar http://localhost/if_var.php. Then the result will look like the following
Explain Conditional Structure in PHP

Program Explanation 4
In the above program, there is the function of isset() which is a function to check whether a variable has been formed (existed) or not. The function will be TRUE if the variable exists. So if the 2nd line of the above program is remarked or omitted then the statement on line 4 will be executed.
Program 5
File Name: switch.php
Description: Program Structure Switch..Case to display day names in English.
Open notepad then type the following php code
<?php
$day = date ("D");
switch ($day) {
 case 'Sun' : $DAY = "Sunday"; break;
 case 'Mon' : $DAY = "Monday"; break;
 case 'Tue' : $DAY = "Tuesday"; break;
 case 'Wed' : $DAY = "Wednesday"; break;
 case 'Thu' : $DAY = "Thursday"; break;
 case 'Fri' : $DAY = "Friday"; break;
 case 'Sat' : $DAY = "Saturday"; break;
 default : $DAY = "Doomsday";
}
echo "Today is the day <b>$DAY</b>";
?>

Explain Conditional Structure in PHP

Save the code with the name switch.php (in the htdocs folder). To see the results please open the browser then in the address bar you type http: //localhost/switch.php. Then the result will be like this.
Explain Conditional Structure in PHP

Program Explanation 5
In the above program, the $day variable (line 2) will contain the first 3 (three) digits of the day name in English. With the date() function, we will get date, time and current information. This function will be further studied in the next Chapter. Next the contents of the $day variable will be checked with a switch (line 3), if the contents are 'Sun' then $DAY are "Sunday" and so on.
Program 6
File Name: if_khusus.php
Description: Special Condition Condition Program? To check for leap years.
Type the following php code into notepad:
<?php
$year = date ("Y");
$kabisat = ($year%4 == 0) ? "KABISAT" : "NOT A KABISAT";
echo "Year <b>$year</b> $kabisat";
?>

Explain Conditional Structure in PHP

Save by name if_specific.php (in the htdocs folder). Then see the results in the browser by typing in the address bar http://localhost/if_khusus.php. Then the result will look like the following.
Explain Conditional Structure in PHP

Program Explanation 6
In the above program, the date() function with the parameter "Y" (line 2) will generate 4 digits of current year. In line 3 it will be checked whether the contents of the $year variable if modulated with 4 will result in a value of 0. If TRUE then $kabisat will contain "KABISAT" and if FALSE $kabisat will contain "NOT KABISAT"
That's my article about Explain conditional structure in php, hopefully can increase our knowledge about php programming. See also article about Comments in PHP Code.

Comments