Switch is for equality comparison only, not for greater than / less than

No less than or greater than statements allowed in switch statements?

Apparently not.

comparison operator in switch statement

WRONG! - this syntax caused a parse error

  switch ($value) {
    case '<20':
      return 1;
    case <100:
      return 5;
    case <200;
      return 10;
    case <500;
      return 20;
    case <2000;
      return 50;     
    default:
      return 100;
  }

Again, the above is BAD. It causes this:

Parse error: syntax error, unexpected '<' in /home/zingspac/public_html/auction2007/modules/contributed/ecommerce/contrib/zing_auction/zing_auction.module on line 107

I don't think there's any way that this is allowed in a switch statement:
http://us.php.net/manual/en/control-structures.switch.php

That's sad.

Is there tricker syntax of which Agaric is not aware?

(For the record, Agaric replaced this switch or if/else idea with an elegant one-line function that divides an item's estimated value by an administrator-chosen factor and rounds up with ciel(), for our latest, greatest Zing Auction Module).

by Benjamin Melançon
Posted on Sun, 2007-09-09 21:53
in
You were close but yes that

You were close but yes that syntax is not correct. Technically you want your switch statement to process for all things TRUE.

switch (TRUE) {
case ($value < 20 ): return 1;
case ($value < 100): return 5;
case ($value < 200): return 10;
case ($value < 500): return 20;
case ($value < 2000): return 50;
default: return 100;
}

So at each case the expression is $value less than 'whatever' will be tested and return either TRUE or FALSE. Therefore your switch statement will be comparing TRUE to TRUE or TRUE to FALSE. I'd imagine that you're
using this similar to an IF/ELSE, so you shouldn't need a break statement.

Posted by rnreekez (not verified) on Thu, 2007-09-27 11:44
Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.