NYCPHP Meetup

NYPHP.org

[nycphp-talk] structured programming in practice

james at surgam.net james at surgam.net
Wed Jun 15 11:17:28 EDT 2005



"Chris Bielanski" says:
...
> My example modified from Dan's: (I went with my habit for case
> syntax

My modification to compact things a bit:

 function make_beverage_cb($bev_type, &$bev) {
     switch ($bev_type) {
         case ('coffee'):
             $result = (add_cwater($bev) && brew_coffee($bev));
             break;
         case ('tea'):
             $result = (add_twater($bev) && add_tea_bag($bev));
             break;
         default: 
             $result = false;
             break;
     }
     return $result;
 }

If you want to stick errors in an array as the original question
asked, you can also do something like this (code off the top of my
head -- NOT TESTED):

 function make_beverage_cb($bev_type, &$bev) {
     $err = array(); // Could be passed in instead
     switch ($bev_type) {
         case ('coffee'):
             $result = ((add_cwater($bev) || (($err[] = 'aw') && false))
		     && (brew_coffee($bev) || (($err[] = 'bc') && false)));
             break;    
         case ('tea'):
             $result = ((add_twater($bev) || (($err[] = 'ac') && false))
		     && (add_tea_bag($bev) || (($err[] = 'at') && false)));
             break;
         default:
             $err[] = 'bad bev';
             $result = false;
             break;
     }
     handle_errs($err);  // or this can be done after returning
     return $result;
 }

$err could also be an array that's passed in and handled later.



More information about the talk mailing list