mardi 9 juin 2015

PHP: Switch() If{} other control structures

How do you execute logic with out the use of Switch or If?

For instance check_id_switch($id)

function check_id_switch($id){
    switch($id){
        case '1': 
        $HW = 'Hello, World!';
        break;
        default:
        $HW = 'Goodbye, World!';
        break;
     } 
  return $HW;
 }

Or instance check_id_if($id)

function check_id_if($id){
    if($id == 1){
     $HW = 'Hello, World!';
    }
   else{ 
   $HW = 'Goodbye, World!';
 }
return $HW;
}

Both of which functions check_id_switch($id) and check_id_if($id) will check the ID to it's reference.

How do I create the same logic as above without using if/switch statements in php? I would also like to avoid forloops.

There are multiple debates regarding performance for the switch/if but if there is another control structure does it under or out perform the aforementioned control structures?

Aucun commentaire:

Enregistrer un commentaire