Gerard-Meier

PHP preventing indention nightmare

Apr 21st, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. // Say you want to validate a few things before you
  2. // run the database query. Here I've used only 3 checks,
  3. // but go figure with 20+ checks! You'll need a HUGE
  4. // widescreen monitor to make it all fit.
  5. if(hasEnoughMoney()) {
  6.     if(hasEnoughIQ()) {
  7.         if(someOtherCheck()) {
  8.             mysql_query("wooot!");
  9.             print "Your order has been placed.";
  10.         } else {
  11.             print "not enough cash";
  12.         }
  13.     } else {
  14.         print "You are too dumb";
  15.     }
  16. } else {
  17.     print "not enough cash";
  18. }
  19.  
  20.  
  21.  
  22. ---------------------------
  23.  
  24. // Using this could be a much cleaner solution:
  25. // the do... while is just there so we can "break"
  26. // the code. You can use a GOTO too, but people seem
  27. // to hate that. (oh, you can use a switch(), too).
  28. do {
  29.  
  30.     if(!hasEnoughMoney()) {
  31.         print "not enough cash";
  32.         break;
  33.     }
  34.  
  35.     if(!hasEnoughIQ()) {
  36.         print "You are too dumb";
  37.         break;
  38.     }
  39.  
  40.     if(!someOtherCheck()) {
  41.         print "some check failed.";
  42.         break;
  43.     }
  44.  
  45.     // All checks passed! Run critical code.
  46.     mysql_query("wooot!");
  47.     print "Your order has been placed.";
  48. } while(false);
Advertisement
Add Comment
Please, Sign In to add comment