Home | | Computer Application 12th Std | PHP Conditional Statements

Chapter: 12th Computer Applications : Chapter 6 : PHP Conditional Statements

PHP Conditional Statements

The following statements in PHP help us to make decisions: if Statement, if...else Statement, if...elseif....else Statement, switch Statement



PHP Conditional Statements:

Like other Programming languages, PHP also allows to write code that perform different actions based on the results of a logical or a test Condition. Thus you can create test conditions in the form of expressions that evaluates to either true or false and based on these results you can perform certain actions. The following statements in PHP help us to make decisions:

if Statement

if...else Statement

if...elseif....else Statement

switch Statement

 

If statement in PHP:

If statement executes a statement or a group of statements if a specific condition is satisfied as per the users expectation. This is the simplest PHP’s conditional statement and can be written in the following form.

Syntax:

if (condition)

{

Statement(s);

}

Example:

<?php

$Pass_Mark=35;

$Student_Mark=70;

if ($Student_Mark>= $Pass_Mark){

echo “The Student is Eligible for the Promotion”;

}

?>

 

If else statement in PHP:

The if statement evaluates a condition and executes a set of code if the condition is true and another set of code if the condition is false.

Syntax:

if (condition)

{

Statement(s) if condition is true;

}

else

{

Statement(s) if condition is false;

}

Example:

<?php

$Pass_Mark=35;

$Student_Mark=70;

if ($Student_Mark>= $Pass_Mark){

echo “The Student is eligible for the promotion”;

}

else {

echo “The Student is not eligible for the promotion”;

}

?>

 

If elseif else statement in PHP:

If- elseif-else statement is a combination of if -else statement. Here multiple conditions can be checked and action is based on the result of the condition.

Syntax:

if (Condition 1)

{

Statement(s) if condition 1 is true;

}

elseif(Condition 2)

{

Statement(s) if condition 2 is true;

}

else

{

Statement(s) if both conditions are false;

}

Example:

<?php

$Pass_Mark=35;

$first_class=60;

$Student_Mark=70;

if ($Student_Mark>= $first_class){

echo “The Student is eligible for the promotion with First Class”; }

elseif ($Student_Mark>= $Pass_Mark){

echo “The Student is eligible for the promotion”;

}

else {

echo “The Student is not eligible for the promotion”;

}

?>

 

Switch Case: 

The switch case is an alternative to the if.. elseif..else statement which executes a block of code corresponding to the match.

Syntax:

switch (n) {

case label1:

code to be executed if n=label1;

break;

case label2:

code to be executed if n=label2;

break;

case label3:

code to be executed if n=label3;

break;

...

default:

code to be executed if n is different from all labels;

}

Example:

<?php

$favcolor = “red”;

switch ($favcolor) {

case “red”:

echo “Your favorite color is red!”;

break;

case “blue”:

echo “Your favorite color is blue!”;

break;

case “green”:

echo “Your favorite color is green!”;

break;

default:

echo “Your favorite color is neither red, blue, nor green!”;

}

?> 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Applications : Chapter 6 : PHP Conditional Statements : PHP Conditional Statements |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.