SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A different method of if statements ?

    I have quite a few if statements I have been hopeing to make more, readable, and understandable, they aren't difficult, just... ugly.. I have seen the "alternate if statements" like $var ? "Stuff" : "blah"; I was wondering if this could be shortened, or done differently? ($room->north is 0 or 1), and $dir is a GET variable, (later will be POST)...

    PHP Code:
    if($dir) {
       if(
    $dir == "north" && $room->north) {
          echo 
    "You are attempting to move north.<br>";
       } elseif(
    $dir == "north" && !$room->north) {
          echo 
    "The room does not have a north here.<br>";
       }
       if(
    $dir == "east" && $room->east) {
          echo 
    "You are attempting to move east.<br>";
       } elseif(
    $dir == "east" && !$room->east) {
          echo 
    "The room does not have a east here.<br>";
       }
       if(
    $dir == "south" && $room->south) {
          echo 
    "You are attempting to move south.<br>";
       } elseif(
    $dir == "south" && !$room->south) {
          echo 
    "The room does not have a south here.<br>";
       }
       if(
    $dir == "west" && $room->south) {
          echo 
    "You are attempting to move west.<br>";
       } elseif(
    $dir == "west" && !$room->west) {
          echo 
    "The room does not have a west here.<br>";
       }
    #Update database here..


    Thank you for any help.

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes there is
    switch($dir) {
    case north:
    echo "BLAH BLAH";
    break;
    case south:
    echo "BLAH BLAH";
    break;
    default:
    echo "BLAH BLAH";
    break;
    }

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you may need to put quotes around the values since they are strings not constants


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
  •