Web Designing Tutorials

What Conditional Statements Used In PHP

Google+ Pinterest LinkedIn Tumblr

Although the control structure determine the flow of code within any programming language. Conditional statements makes logical decision between various conditions based on input values. So the conditional statements makes your computer program to respond according to their inputs. Similar to other programming languages, PHP also have different types of conditional statements. The conditional statements used in PHP are if statement, if – else statement, else if statement and switch statement.

Before starting to learn on different types of conditional statements, lets start with reading different types of operators used in PHP, if you are not already been.

If Statement

Among all of the conditional statements used in PHP, if statement is the most commonly used conditional statement. It offers a conditional code execution. Following is the syntax that shows the correct ways of using if statement in PHP.

if(expression) {statement}

Here is an example that shows the uses of if statement. In this example the company will give the bonus 10% of basic salary if the basic salary is greater than 6500.

<?php

$b_salary=8000;

if ($b_salary>6500) {

$salary=$b_salary*10/100;

echo “The total salary is $salary”;

}

?>

Output:

The total salary is 800

Else Statement

Since, the uses of else statement along with if statement allows you to insert one more condition than default if condition. The else statement outputs the results for rest of the conditions that do not match on if condition.

Let’s see again the previous example where the additional else statement gives the output if the salary is less then 6500.

<?php

$b_salary=8000;

if ($b_salary>6500) {

$t_salary=$b_salary*110/100;

echo “The total salary is $t_salary”;

}

else {

$t_salary=$b_salary + 500;

echo “The total salary is $t_salary”;

}

?>

Output:

The total salary is 8800

ElseIf Statement

You have already know that if condition is used for single condition and two conditions can be implemented on if else statement. But if you need to generate outputs for more then two conditions, the elseif statement is required.

Following is an example that shows the uses of elseif statement in PHP. The PHP code written in this example displays the division obtained by students according the percentage they have achieved.

<?php

$percentage=66;

if ($percentage>=60) {

echo “First Division”;

}

elseif ($percentage>=45) {

echo “Second Division”;

}

else {

echo “Pass Division”;

}

?>

Output:

First Division

Switch Statement

Since, the switch statement is slightly different than other conditional statements like if, if … else, if …. elseif ……. else statements. The switch statement is required when you need to generate outputs with comparing values of the variables which have multiple number of values.

Here is an example that shows the implementation of switch case statement in PHP.

<?php

$day=3;

switch($day) {

case 1:

echo “It was Sunday”;

break;

case 2:

echo “It was Monday”;

case 3:

echo “It was Tuesday”;

break;

default:

echo “It was other than Sunday, Monday and Tuesday”;

}

?>

Output:

It was Tuesday

In the above example the break statement is inserted at the conclusion of each case block. If the break statement is not placed there all the occurrences of case statements are executed.

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 5 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Write A Comment