As in life it happens to have to choose which road to take in front of a crossroads, so does in programming.

Since variables are one of the basic elements of the instructions and they can have different values, we may want to test these values and take different paths for two reasons:

  1. Some paths create errors or security problems that need to be addressed and solved
  2. Different logics must be applied on the various roads

If-then-else

It is the most commonly used conditional construct because the most common one: the junction. When you have two alternatives, you choose the variable that contains the discriminant of the choice — the one that can take either true or false. Based on this truth value the program takes only one of the two roads.

$condizione = true || false
if ($condizione) {
  //condition block if true
} else {
  //condition block if false
}

In some programming languages, the then keyword is explicated after the condition. In PHP and Javascript the blocks are separated instead from the curly braces.

The block enclosing the condition if true is mandatory. If no instructions are provided in this block, you can leave it blank. The Else block, however, is optional.

$condizione = true || false
if ($condizione) {
  //condition block if true
} 

if ($condizione) //condition line if true

If you have only one instruction to write you can not enclose it between braces, although doing so slightly penalizes the simplicity of reading.

Elseif

If the condition is false, we can apply a control to a new variable. For brevity the two constructs can be grouped into an elseif.

$condizione = true || false;
$condizione2 = true || false;

if ($condizione) {
  //first condition true (second contition non checked)
} elseif ($condizione2) {
  //first condition false, second contition true
} else {
  //both condition false
}

Switch-Case-default

The other construct that serves to indicate multiple choices to accomplish based on the value of a variable is theswitch.

This construct parses a variable and, for each of the values it might take, you can create a custom block of code. In each of these blocks, called cases, the exact value of the variable is checked. No boolean operator can be used.

$var = rand(0, 3);

switch ($var) {
  case 1:
    //block executed if $var == 1
    break;
    
  default:
    //block executed if non of the cases are exeuted
}

If a case is executed, all subsequent cases are executed unless, as you often do, you do not interrupt the switch switch execution with a break.

If none of thecases are run, theswitch will execute the optional default lock.

Now you try

<?php

function testSwitch() {
  $var = rand(0, 3);
  $value1 = 1;
  $value2 = 2;
  $value3 = 3;

  switch ($var) {
    case $value1:
      //block executed if $var == $valore1
      echo "Run block 1";
      break;
    case $value2:
    case $value3:
      //block executed if $var == $value2 || $var == $value3

      echo "Run block 2 e 3";
      break;

    default:
      //block executed if non of the cases are executed
      echo "Run default block";
  }
}

echo testSwitch();

?>
-
0 0 votes
Article Rating
Notifiche
Notificami
0 Comments
Inline Feedbacks
View all comments