0

If Statements

September 19, 2022
Share

Most times we need to carryout decisions in our programs and to do this, we use if statements. To use if statements, we normally use comparison and logical operators because we are comparing values much of the time.

if .. else ..

Let us first get this example of a program that determines if the day is a weekend or a weekday.

<?php

	$day = Monday;

	if ($day == "Sunday") {
		echo "It is a weekend.";
	} else {
		echo "It is a weekday.";
	}

?>

To use the if statement, must follow if with a condition that is put in brackets (for our case it is $day == “Sunday”. This is followed with curly brackets where we write our first decision (output to the screen that the day is a weekend). The we close the first decision with the curly bracket. We use the else keyword followed by curly bracket to bring another decision in case the first condition is otherwise based on the value supplied in the $day variable. Therefore, if the value in the $day is any other day other than Sunday, the program will output it is a weekday and the reverse is true. You can try to change the value in $day variable to Sunday and see the change.

Example 2: Let us get our example of a program that determines if student can do exams basing on the amount of tuition paid. If the student’s balance is 500,000 and above, then the student will not be allowed to do exams.

<?php

	$tuition = 1500000;
	$amountPaid = 700000;
	$balance = $tuition - $amountPaid;


	if ($balance >= 500000) {
		echo "You are not allowed to do exams.";
	} else {
		echo "You are allowed to do exams.";
	}

?>

From the code above, we can see that this student will not be allowed to do exams because they have only paid 700,000 out of 1,500,000 remaining with a balance of 800000 which is above 500,000.

if .. else if .. else

So far, we have only looked at making decisions based on only two scenarios i.e., if one if true, the other is not. But what if we have multiple decisions to make based on different scenarios for example if the student’s tuition balance is 500,000 and above, they are allowed to do 2 papers. If the balance is 900,000 and above, they are allowed to do one paper. If the balance is 1,200,000 they are not allowed to do any paper or else they are allowed to do all papers in case all the tuition is paid.

This is when we use the if .. else if .. else statement. No difference with the first one but the only part included is the else if that is used to introduce in some other conditions as seen in the snippet below.

<?php

	$tuition = 1500000;
	$amountPaid = 700000;
	$balance = $tuition - $amountPaid;

	echo "The balance is $balance";

	if ($balance == 500000) {
		echo "You are allowed to do two papers.";
	} else if ($balance >= 900000){
		echo "You are allowed to do one paper.";
	} else if ($balance >= 1200000) {
		echo "You are not allowed to do any paper.";
	} else {
		echo "You are allowed to do all papers.";
	}

?>

Example 2: Let us write a program that will be used to determine the student grade based on the score. if the student’s score is above 80, then it is an A, if it is 70 to 79, then it is a B, it is 60 to 69, then it is a C, if it is 50 to 59 and above, then it is a D and finally if it is any other score, it is an F.

<?php 

	$name = "Kabendera Asinah"; 
	$markObtained = 90.2;

	if ($markObtained >= 80) {
		echo "Your grade is A.";
	} elseif ($markObtained >= 70 and $markObtained <= 79) {
		echo "Your grade is B.";
	} elseif ($markObtained >= 60 and $markObtained <= 69) {
		echo "Your grade is C.";
	} elseif ($markObtained >= 50 and $markObtained <= 59) {
		echo "Your grade is D.";
	} else {
		echo "Your grade is F.";
	}

?>

Congratulations 🎉. You have learnt how to use if statements in PHP. Continue to explore the new world full of possibilities with PHP. 🙉