switch


php128 apg

Behavior similar to if ... elseif ... else.

Is a very important language construction, because it allows decision making during the programming process.





Of course, there are other implementations - more complex and specific - for conditional evaluation.

Right now, what we teach you is enough.

We shall certainly return to the matter soon.



FIRST SYNTAX TYPE


In the following, we will show the FIRST SYNTAX TYPE of -  switch  - conditional structure:



<?php

// STRUCTURE #1

switch (var)
    {
    case 
val 1:
    
statement 1
    
break;
    
    case 
val 2:
    
statement 2
    
break;
    
. . . . . . . . . . . .

    case 
val J:
    
statement J
    
break;
    
. . . . . . . . . . . .

    default:
    default 
statement
    
}

?>


Each of the case structures are evaluated according to a BOOLEAN type.

01 .  statement 1  will be executed...

      If the  case val 1:  is verified as TRUE

02 .  statement 2  executed...

      If  case val 1:  is verified as FALSE.

03 .  statement 3  executed...

      If  case val 1:  is verified as FALSE
and
          case val 2:  is verified as FALSE.

04 .  statement 3  executed...

      If  case val 1:  is verified as FALSE
and
          case val 2:  is verified as FALSE
and
          case val 3:  is verified as FALSE.

And so on...

If all  case val X:  are evaluated as FALSE, then the default statement will be executed.

0N .  default statement  executed...

 default:  always TRUE.


SECOND SYNTAX TYPE


In the following, we will show the SECOND SYNTAX TYPE of -  switch  - conditional structure:



<?php

// STRUCTURE #2

switch (var):

    case 
val 1:
    
statement 1
    
break;
    
    case 
val 2:
    
statement 2
    
break;
    
. . . . . . . . . . . .

    case 
val J:
    
statement J
    
break;
    
. . . . . . . . . . . .

    default:
    default 
statement
    
    
endswitch;

?>


Each of the case structures are evaluated according to a BOOLEAN type.

01 .  statement 1  will be executed...

      If the  case val 1:  is verified as TRUE

02 .  statement 2  executed...

      If  case val 1:  is verified as FALSE.

03 .  statement 3  executed...

      If  case val 1:  is verified as FALSE
and
          case val 2:  is verified as FALSE.

04 .  statement 3  executed...

      If  case val 1:  is verified as FALSE
and
          case val 2:  is verified as FALSE
and
          case val 3:  is verified as FALSE.

And so on...

If all  case val X:  are evaluated as FALSE, then the default statement will be executed.

0N .  default statement  executed...

 default:  always TRUE.

 endswitch; 


THIRD SYNTAX TYPE


In the following, we will show the THIRD SYNTAX TYPE of -  switch  - conditional structure:



<?php

// STRUCTURE #3

switch (var)
    {
    case 
val 1;
    case 
val 2;
    case 
val 3;
    . . . . . . . .
    case 
val J;
    . . . . . . . .
    
statement 123
    
break;
    
    default:
    default 
statement
    
break;
    }

?>


Each of the case structures are evaluated according to a BOOLEAN type.

01 .  statement 1  will be executed ...

      If  case val 1  is TRUE
    OR
            case val 2  is TRUE
    OR
               case val 3  is TRUE
              . . . . . . . . . . . . . . . . . . . .
    OR
               case val J  is TRUE

0N .  default statement  will be executed ...

      If all previous  case val 1, val2, ... etc.  are evaluated as FALSE.




 SOLVE ALL PROPOSED EXERCISES 


             NOT SOLVED YET             


If possible in several versions of PHP


Especially the latest 7 and 8


Study the displayed data carefully


  1 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -

   Data collected by some input process 
   in the variable $d01
   
   - - - - - - - - - - - - - - - - - - - - - - - */

$d01 'Isaac Newton';

switch (
$d01)

{
case 
'Socrates':
echo 
$d01 ', the philosopher!';
break;

case 
'Isaac Newton':
echo 
$d01 ', the physicist!';
break;

case 
'Mariah Carey':
echo 
$d01 ', the singer!';
break;

default:
echo 
'Not related!';
}

?> 

 RESULT   

Isaac Newton, the physicist!


  2 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -

   Data collected by some input process 
   in the variable $d02
   
   - - - - - - - - - - - - - - - - - - - - - - - */

$d02 'Socrates';

switch (
$d02):

case 
'Socrates':
echo 
$d02 ', the philosopher!';
break;

case 
'Isaac Newton':
echo 
$d02 ', the physicist!';
break;

case 
'Mariah Carey':
echo 
$d02 ', the singer!';
break;

default:
echo 
'Not related!';

endswitch;

?> 

 RESULT   

Socrates, the philosopher!


  3 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -

   Data collected by some input process 
   in the variable $d03
   
   - - - - - - - - - - - - - - - - - - - - - - - */

$d03 'Mariah Carey';

switch (
$d03)
{
case 
'Socrates';
case 
'Isaac Newton';
case 
'Mariah Carey';
echo 
$d03 ', Is related!';
break;

default:
echo 
'Not related!';
}

?> 

  4 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -

   Data collected by some input process 
   in the variable $d04
   
   - - - - - - - - - - - - - - - - - - - - - - - */

$d04 'Red';

switch (
$d04):

case 
'Red';
case 
'Green';
case 
'Blue';
echo 
$d04 ', Is related!';
break;

default:
echo 
'Not related!';

endswitch;

?>