User login

How to use Less Than or Greater Than in a switch statement

Solution

(From rnreekez in the comments, syntax modified to conform to Drupal's more verbose coding standards.)

Technically you want your switch statement to process for all things TRUE.

<?php
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;
}
?>

Background

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 trickier 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 ceil(), for our latest, greatest Zing Auction Module).

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

Comments

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.

Case Switch - Reply to Comment

Wow! I have been working with web design, and coding dynamic data for the last 4 years, and I never knew you could do this.

It just helped me finish coding the logic for a paging method, I've been working on. I will be sure to remember this for the future. It will certainly make things easier.

4 Years Later...

Well, I see it's four years later, but I just stumbled on this and it's good stuff. Thanks for the tip @rnreekez!

This was really useful for me

This was really useful for me too. Thanks for sharing!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.