2013-02-14

'If' and 'switch' as Convertible Conditionals!~

In a previous post, I explained how the conditional statement 'if' syntax looks like. It is very useful when we are writing different programs, but sometimes, when the code is more complicated, it starts to look like a very messy code. Therefore, we can alternatively use convertible conditional statement 'switch', whose syntax I wrote below:

                                                 <?php

                                                 switch($variable){
                                                           case 'value';
                                                                     // do something
                                                                     break;
                                                           default:
                                                                    // do something else
                                                 }
                                                 ?>

Let's write here, piece of code, by using conditional statement 'if': 

                                                 <?php
 
                                                 $day='Thursday';

                                                 if($day=='Monday') {
                                                          echo 'Today is Monday!';
                                                 } elseif($day=='Tuesday') {

                                                          echo 'Today is Tuesday!'; 
                                                 } elseif($day=='Wednesday') {
                                                          echo 'Today is Wednesday!'; 
                                                 } elseif($day=='Thursday') {
                                                          echo 'Today is Thursday!'; 
                                                 } elseif($day=='Friday') {
                                                          echo 'Today is Friday!';
                                                 } else {
                                                          echo 'Today is Weekend!';
                                                 }
                                                 ?>

As soon as I wrote here something by using statement 'if', you can see that statement 'switch' can be use in the same way and this piece of code that I wrote you, I can write in a different way, by using the conditional 'switch':


                                                 <?php
 
                                                 $day='Thursday';

                                                 switch($day) {    

                                                        case 'Monday':
                                                                     echo 'Today is Monday!'; 
                                                                     break
                                                        case 'Tuesday':
                                                                     echo 'Today is Tuesday!'; 
                                                                     break
                                                        case 'Wednesday':
                                                                     echo 'Today is Wednesday!'; 
                                                                     break;  
                                                        case 'Thursday':
                                                                     echo 'Today is Thursday!'; 
                                                                     break
                                                        case 'Friday':
                                                                     echo 'Today is Friday!'; 
                                                                     break
                                                        default:
                                                                     echo 'Today is Weekend!'; 
                                                   }
                                                   ?>

For sure, you can see, the differences between 'if' and 'switch' conditional statements. Although 'switch' requires to be break after every instruction, but on the other hand, it needs only one bracket, due to which the code looks much more better ;)

Brak komentarzy:

Prześlij komentarz

Dziękuję Ci za poświęcony czas i pozostawienie komentarza :)