Re: control structure question

From: Date: Tue, 23 Jul 2002 15:06:20 +0000
Subject: Re: control structure question
Groups: php.general 
Request: Send a blank email to php-general+get-109085@lists.php.net to get a copy of this message
So refering back, i re-wrote the original example using the switch syntax... switch (true) { case doStep1(): case doStep2(): case doStep3(): error(); break; default: valid(); } Each case expressions is evaluated, until one of them evaluates to a value equal (==) to the switch expression (in this case a boolean error flag). The error() code will only be called if one of the doStep() evaluates to false. And the valid() code will only be evaluated if the switch reached the default, which means that none of the above check returned false I think for this example the switch syntax is more elegant than using a series of if() statements. Thanks, Javier
For steps and sequences, I always use switches. Most commonly associated with the "action" variable, it would look like this: switch($action) { default:
      // what to show if no variable exists, or is set to a value
      // that is not acceptable below
break; case "add_file":
      // what to do in the even that a file needs to be added
      // to the database.
break; case "remove_file":
      // what to do in the even that a file need sto be removed
      // from the database.
break; } Basically, whatever the value of $action, that is what section of code will execute. Hope that helps! Martin Clifford Homepage: http://www.completesource.net Developer's Forums: http://www.completesource.net/forums/
"Javier Montserat" <codareef@hotmail.com> 07/23/02 10:03AM >>>
is there a more programmatically elegant way of saying... $isError = ""; function main() {
    doStep1();
    if (!$isError) {
        doStep2();
    }
    if (!$isError) {
        doStep3();
    }
    // etc. etc.
} function doStep1() {
    if ($something) {
        $isError = 1;
    }
} function doStep2() {
    if ($something) {
        $isError = 1;
    }
} _________________________________________________________________ Join the world's largest e-mail service with MSN Hotmail. http://www.hotmail.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
_________________________________________________________________ Chat with friends online, try MSN Messenger: http://messenger.msn.com

« previous php.general (#109085) next »