It is intended to be used when programming requires different instructions to be processed rather than sequentially.
Code that executes according to the true or false of a given condition is selected to execute different instructions. Control statements typically include if statements and switch statements.
The words above are very difficult.
Let's understand by looking at the source
The if statement executes the next command if the given condition is true and not if it is false.
For example, if the person who created Everever is a person, it is true, so it executes the command to execute if it is true, otherwise it does not.
When using the expression equals in conditional statements, ==
<?php $a = 50; if($a == 50){ echo "hi"; } ?>
Did you enter the value of variable a as 50 in the source above?
And if the condition of variable a is 50 in the if statement, and it is true, it executes ehco "hi";
The above results show 50 in a web browser.
Now let's create a situation where the if statement is false.
Let's make a condition where $a == 40. Then, let's say that a is 50 but when 40, echo "hi";
<?php $a = 50; if($a == 40){ echo "hi"; } ?>
For a source like this, since a is not 40, it does not represent anything.
The else statement is a statement used with the if statement.
Above we saw that if the if statement is true, we print hi.
Use else to give a command if it is not true
<?php echo "The odds and evens of the multiplication result of \$a = 15 and \$b = 23 <br />"; echo "<hr />"; $a = 15; $b = 23; $gob = $a * $b; if($gob %2 == 0) { echo "The value of $a * $b is even. Result {$gob} <br />";
} else { echo "The value of $a * $b is odd. Result {$gob} <br />";
} ?>
The source above tells you how many odd or even numbers you have.
The product of a and b is called $gob and $gob is divided by 2.
If the remainder is 0, it is even, and if the remainder is not 0, it is odd.
If you test by changing the values of a and b in the source above, you will get different results.
The sources below are those who pass the graduation exam with a score of 80 or higher in their major.
<?php echo "Graduate at least 80 majors and 75 or more liberal arts <br />"; $var1 = 84; // major $var2 = 75; // liberal arts if($var1 >= 80 && $var2 >= 75) { echo "Major $var1 points, liberal arts $var2 points You passed the graduation exam. <br />"; } else { echo "Major $var1points, liberal arts $var2 points You have not passed the graduation exam. Please enter 5th grade. <br />";
} ?>
In the above expression, && is inside the if statement.
This && says that both conditions are true when both conditions are true.
For example, $var >= 80 This must be true, and $var >= 75 must be true.
The else statement will be executed. The && can be rewritten as and, but in the business use &&, use &&.
Otherwise, execute the echo statement of the else statement.
If you want to be able to graduate if only one of the two conditions is met, you can use ||(or) instead of &&(and).
Below is the result
The else if statement is used when a condition must be used.
<?php echo "Save admission fee <br />"; echo "------------------------------------------ <br />"; echo "50% younger than 10 years old, 80% younger than 65 years old <br />"; $fee = 5000; // admission fee is 5000 $age = 88; // age 88 $sol = "soldier"; // set as identity soldier if($sol == "citizen") { echo "$sol is a free admission. <br />"; // sol is a soldier, so the condition is a citizen, so we move on to the else statement. } else if($age <= 10) { $fee *= 0.5;// multiply by 0.5 to get half the price. echo " {$age}is a child, so 50% of 5000 * 0.5 = $fee to enter. <br />"; } else if($age >=65) { // condition is not true in the above statement, another condition is needed and condition is recreated by declaring elseif statement $fee *= 0.2; // multiply by 0.2 to get 80% off echo "The {$age} year is 80% off the route 5,000 * 0.2(1-0.8) = {$fee} <br />"; echo "You pay only {$fee}. <br />"; } else { // If the above conditions are not met, then the command below ... echo "The basic entrance fee is 5,000<br />"; } ?>
This time it is a program that assigns a grade based on the score.
<?php echo "<b>Find the total score, average, and grade of 3 courses</b><br />"; $math = 100; //Math $elec_circuit = 80; //electronic circuit $design = 77; // design $sum = $math + $elec_circuit + $design; // sum of three subjects $avg = $sum/3; // divide the three by three then average echo " math : {$math} <br />"; echo " electronic circuit : {$elec_circuit} <br />"; echo " design : {$design} <br />"; echo " total : {$sum} <br />"; echo " average : {$avg} <br />"; echo " your average point "; if($avg >= 95 && $avg<=100) { // If all the conditions above 95 and 100 are met, the following sentence is printed. echo "A+"; } else if($avg >= 95 && $avg <=94){ // You can use and instead of &&. Because there is elseif statement, it's convenient not to do if after else. Not much difference ;;;
echo "A0"; } else if($avg >= 85 && $avg <=94) { echo "B+"; } else if($avg >= 80 && $avg <=84) { echo "B0"; } else if($avg >= 75 && $avg <=79) { echo "C+"; } else if($avg >= 70 && $avg <=74) { echo "C0"; } else if($avg >= 65 && $avg <=69) { echo "D+"; } else if($avg >= 60 && $avg <=64) { echo "D0"; } else { echo "F"; } ?>
Maybe you don't know
If there is more than one line in the condition of if($a == 100), enclose it in {}
In one line
Just use it.
For example
if($a == 100) {
echo "Really good";
echo "Please try again";
}
Tie the stomach like that
if($a == 100)
echo "Very nice, try again later";
The stomach does not have to be tied.
The result of the above source is:
Thank you for visiting. If you have any inquiry or explanation of mistakes, please use the comments below.
//=$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']?>ALL COMMENTS 0
Sort by