Re: [PHP] control structure question
On Tue, 23 Jul 2002, Javier Montserat wrote: 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. That's pretty clever! miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] control structure question
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 worlds 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
Re: [PHP] control structure question
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 [EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] control structure question
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 [EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php