DEV Community

Abdulkareem Mustopha
Abdulkareem Mustopha

Posted on • Updated on

Module 3 of #100DaysOfPHP: Decision Making in PHP

PHP allows us to perform actions based on some type of conditions that may be logical or comparative. Based on the result of these conditions i.e., either TRUE or FALSE, an action would be performed as asked by the user. It’s just like a two-way path. If you want something then go this way or else turn that way.

To use this feature, PHP provides us with four conditional statements:

  • if statement
  • if…else statement
  • if…elseif…else statement
  • switch statement

Let's look at how each conditional statements work

1. if Statement

This statement allows us to set a condition. On being TRUE, the following block of code enclosed within the if clause will be executed.

Syntax
if (condition){ // if TRUE then execute this code }

Example
<?php
$x = 12;
if ( $x > 0)
{ echo "The number is positive" ; } ?>

Output
The number is positive

2. if…else Statement

We understood that if a condition will hold i.e., TRUE, then the block of code within if will be executed. But what if the condition is not TRUE and we want to perform an action? This is where else comes into play. If a condition is TRUE then if block gets executed, otherwise else block gets executed.

Syntax
if (condition)
{ // if TRUE then execute this code }
else{ // if FALSE then execute this code
}

Example
<?php
$x = -12;
if ( $x > 0)
{ echo "The number is positive" ; }
else{ echo "The number is negative" ; }
?>

Output
The number is negative

3. if…elseif…else Statement

This allows us to use multiple if…else statments. We use this when there are multiple conditions of TRUE cases.

Syntax
if (condition) { // if TRUE then execute this code }
elseif { // if TRUE then execute this code }
elseif { // if TRUE then execute this code }
else { // if FALSE then execute this code }

Example
<?php
$x = "August" ;
if ( $x == "January" )
{ echo "Happy Republic Day" ; }
elseif ( $x == "August" )
{ echo "Happy Independence Day!!!" ; }
else{ echo "Nothing to show" ; } ?>

Output
Happy Independence Day!!!

4. Switch Case Statement

The switch statement is used to perform different actions based on different conditions.

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!”; }
?>

Jump Statements

Break Statement

In PHP or any programming language, if any specific condition is true, then the break statement is used to stop the program execution immediately and exit the program.

The loop or switch statement use break statement in PHP

Example
<?php for($i=1; $i<=10;$i++) if($i==6){ break; } echo $i, “
”; }
?>

Continue Statement

PHP or any programming language, if any specific condition is true, then continue statement skip the program execution and go to the next program. Example <?php for ($i=1<=10;$i++){ if($i==6){ continue; } echo $i, “
” ; }

Exit Statement###

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called. The message to be displayed is passed as a parameter to the exit() function and it terminates the script and displays the message. The exit() function is an alias of the die() function.

Example
<?php //declaring variables
$a=5; $b=5.0;
if ($a==$b) { //terminating script with a message using exit() exit('variables are equal'); } else {
//terminating script with a message using exit() exit('variables are not equal'); }
?>

Output variables are equal

The journey is getting more interesting and tasking, getting lot of errors while practicing and learning lot of things. I will appreciate if you can suggest a resource to get PHP mini projects (requiring basic knowledge) to work on, I think that the will go a long way to help in the field, Thanks!

Top comments (0)