PHP: Decisions

PHP has three main ways to implement decisions in programs.

  1. If statements
  2. Case statements
  3. Ternary operator

Decision statments allow one to have certain code executed only under certain condition(s). Another way to think of decision statements is branches, i.e. go down this path and execute this code if this condition is true. Otherwise, execute a different block of code.

A block of code is executed only if a condition is true. A condition is represented through what is known as an expression. These expressions are evaluated using boolean logic, meaning their result can be eithe true or false.

Example: Simple if statement

In this example, the expression is $salary >= 45000. Is this condition/ expression true? Yes it is. Thefore the line of code under the if statement is executed.


	<?php
	$salary = 50000;
	if ($salary >= 45000)
		echo "<p>You are in salary grade S4.</p>":
	?>
	

Run it now


Example: Simple if statement

If you want to have more than one line of code executed under an if statement in PHP you must use curly braces as shown below. This is known as a "code block". $student_id == 193812 is the expression in the example below.


	<:?php
	$student_id=193812;
	if ($student_id == 193812) 
	{
		echo "<:p>You are a Senior.<:/p>";
		echo "<:p>Be sure to apply for graduation.<:/p>";
	}
	echo "<:p>This statement is not in the If statement<:/p>";
	?>
	

Run it now

Output:

  You are a Senior.

  Be sure to apply for graduation.

  This statement is not in the If statement


Relational Operators

In the previous example, == was used in the expression to test the condition. == is a relational operator. PHP supports the following relational operators:

Operator Meaning
== Equal
=== Identical
!= Not equal
<> Not equal
!== Not identical
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Example: Simple if-else statement with AND logical operator


	<?php
	$partno = 234091;
	$quantity = 1050;
	if ($partno == 234091 and $quantity > 1000)
	{
		echo "<p>Discount available</p>";
		echo "<p><a href=\"http://www.amazon.com/\">Order now</a></p>";
	}
	else
	{
		echo "<p>Discount not available

"; echo "<p><a href=\"http://www.bestbuy.com/\">Call for pricing</a></p>"; } ?>

Run it now

Output:

  Discount available
  Order now


Logical Operators

The expression in the previous example utilized the "and" logical operator. PHP supports the following logical operators:

Operator Meaning
and True if both expressions are true
or True if either expression is true
xor True if either expression is true, but not both
&& And; True if both expressions are true
|| Or; True if either expression is true
! True if expression is NOT true

Example: Simple if-else statement with OR logical operator


	<?php
	$gpa = 3.1;
	$major = "Computer Information Systems";
	
	if ($gpa > 3.2 or $major == "Computer Information Systems" or $major == "Cisco")
	{
		echo "<p>You are eligible for the scholarship.</p>";
		$amount = 500;
	}
	?>
	

Run it now

Output:

  You are eligible for the scholarship.


Example: Simple if statement

Open in new window


Example: Simple if-elseif statement


	<?php
	echo "<p>Grade Calculator</p>";
	$grade = 87.5;
	if ($grade < 60)
		echo "<p>Your grade is an E</p>";
	elseif ($grade < 70)
		echo "<p>Your grade is an D</p>";
	elseif ($grade < 80)
		echo "<p>Your grade is an C</p>";
	elseif ($grade < 90)
		echo "<p>Your grade is an B</p>";
	elseif ($grade < 100)
		echo "<p>Your grade is an A<p>";
	elseif ($grade == 100)
		echo "<p>Your grade is an A+</p>";
	?>
	

Run it now

Output:

  Grade Calculator

Your grade is an B


Example: Simple if-elseif statement

In programming, there is often many more than one way to code the same programming and produce the same output/functionality. However, some methods are more efficient and some methods are easier to read.


	<?php
	echo "<p>Grade Calculator</p>";
	$grade = 87.5;
	if ($grade > 100)
		echo "<p>Your grade is an A+</p>";
	elseif ($grade >= 90)
		echo "<p>Your grade is an A</p>";
	elseif ($grade >= 80)
		echo "<p>Your grade is an B</p>";
	elseif ($grade >= 790)
		echo "<p>Your grade is an C</p>";
	elseif ($grade >= 60)
		echo "<p>Your grade is an D</p>";
	else
		echo "<p>Your grade is an E</p>";
	?>
	

Run it now

Output:

  Grade Calculator

Your grade is an B


Example: Nested if statement


	<?php
	   $person1="Phil";
	   $person1_age = 35;
	   $person2="Tina";
	   $person2_age = 32;
	   if ($person1 == "Phil")
	   {
			  echo "<p>Your name is $person1.</p>";
			  if ($person1_age > $pesron2_age)
					echo "<p>$person1 is older than $person2.</p>";
			  else
					echo "<p>$person2 is older than $person1.</p>";
		}
	   else
	   {
			 echo "<p>Your name is person2.</p>";
			 echo "<p>Age is only a number!</p>";
	   }
	?>
	

Run it now

Output:

  Your name is Phil.

  Phil is older than Tina.