SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Greater/Lesser than a Numeral (PHP Switch)

    Why does the switch below give me an "unexpected '>'" error? I Google'd for examples and found some that write it case > $numeral and others that write it case > = $numeral, but both give me the same error.

    I want to display a centered image ($Img1C) if that image is more than 250 pixels wide, with the default image size floated to the right ($Img1R).

    Thanks.

    PHP Code:
    switch($Width1)
    {
     case > 
    250:
     
    $Img1st $Img1C;
     break;
     default:
     
    $Img1st $Img1R;
     break;


  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i'm not sure where you saw those samples, but they must not have been php. here's the supported syntax of switch: http://us2.php.net/manual/en/control...res.switch.php

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by longneck View Post
    i'm not sure where you saw those samples, but they must not have been php. here's the supported syntax of switch: http://us2.php.net/manual/en/control...res.switch.php
    Thanks, but there's only one mention of a greater than/lesser than value on that page...

    PHP Code:
    <?php
    switch ($i) {
    case 
    0:
    case 
    1:
    case 
    2:
        echo 
    "i is less than 3 but not negative";
        break;
    case 
    3:
        echo 
    "i is 3";
    }
    ?>
    I tried a lame experiment to change it to a switch that says "anything less than 250"...

    PHP Code:
    switch ($Width1)
    {
     case 
    0-249:
     echo 
    "Width is less than 250";
     break;

     default:
     echo 
    'DEFAULT';
     break;

    But it doesn't work.

    The Manual has a page of "Comparison Operators," with $a > $b apparently working with IF functions. Is there a way to adapt this to a PHP switch?

    Thanks.

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2007
    Location
    Australia
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AFAIK, this can't be done in a PHP switch. You can use if/else statements, and if you want slightly cleaner syntax, try the alternative syntax (e.g. if(statement): ... endif; ). The switch structure is only for easily executing code based on known values of a statement/variable.

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    This can be done 1 sec
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    switch (true) { # Pay close attention to the true.
        
    case $Width1 250# if true, enter; if false, skips;
            
    $Img1st $Img1C;
        break;
        
        default:
            
    $Img1st $Img1R;
        break;
    }
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It can, but we shouldn't encourage geosite to continue this pseudo-language he is creating where switch() trees control all program flow whether appropriate or not.

    PHP Code:
    //3 lines with if()
    $Img1st $Img1R;
    if (
    $Width1 250)
      
    $Img1st $Img1C;

    //1 line with ternary operator
    $Img1st = ($Width1 250) ? $Img1C $Img1R

  9. #9
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the elegant solution, Dan.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •