The Conditional Sentences

The Conditional Sentences

Conditional sentences are a very important component in our code

So far, we have seen how we can create simple code in PHP. But, if we talk about conditions, do you know how to create conditional sentences in PHP? For example, to execute a block of code if a certain condition is met?

Conditional sentences allow our script to decide what to do, evaluating if certain condition inside our code is met. Depending on this result a block of code will be executed.

The IF Statement

To create a conditional sentence in PHP, we use the IF statement, like this:

<?php

if(  /* condition */  ){
# code to be executed
} 
?>

The ELSE Statement

If we are already evaluating a condition, and we want to execute another block of code if that condition is not met, then we can use the ELSE statement alongside the IF statement.

<?php

if(  /* condition */  ){
# code to be executed if condition is met
} else {
# code to be executed if condition is not met
}

?>

The ELSE IF Statement

Both previous statements can be combined to enhance the evaluation of conditions inside our code, allowing us to be more descriptive about them and also to create very specific cases.

<?php

if(  /* condition */  ){
# code to be executed if condition is met
} 
else if ( /* condition A */ ) {
# code to be executed is condition A is met
}
else if ( /* condition B */ ) {
# code to be executed is condition B is met
}
else {
# code to be executed if no condition is met
}

?>

Nested IF Statements

We can also put an IF statement inside another IF statement. In this case, we have to be very aware of the different possibilities of what can happen with our code inside this structure.

<?php

if(  /* condition */  ){
# code to be executed if condition is met
     if ( /* condition A */ ) {
        # code to be executed is condition A is met
     }
} 
else {
# code to be executed if condition is not met
     if ( /* condition B */ ) {
         # code to be executed is condition B is met
     }
}
?>

Evaluating ranges with IF Statements

We can use conditional sentences to evaluate if a variable is inside (or outside) certain range of values, as follows

<?php

$ age = 18;

if ($age < 20){
# execute this block
}

if ($age > 20 && $age <= 35){
# execute this other block
}

?>

The SWITCH Statement

If we don't want to pollute our code with a bunch of IF statements, we can use the SWITCH structure. The main difference is that SWITCH only evaluates a single condition. It will then execute the block of code when it obtains a true result and find a BREAK statement.

When none of the conditions are met, the "default" option will be executed.

<?php

$color = 'blue';

switch($color){
     case 'yellow':
            print 'This color is yellow';
            break;
      case 'red':
            print 'This color is red';
            break;
      case 'green':
            print 'This color is green';
            break;
      case 'blue':
            print 'This color is blue';
            break;
       default:
            print 'This color is black';
}

?>