SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Modesto, CA
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if else for 10items

    Basicly Im doing this..

    PHP Code:
    if ($nagi == "0") {
        }
        else {
            (
    $nagi "1");
        }

    ?> 
    I have 15 $nstuff to do it to, can I make a if else statment for them all in one try.. or do I have to right that out for every one?

  2. #2
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why not use a "switch...case" statement and save yourself a little processing time and make your code more readable at the same time. You can also add in some error handling using the default clause as well.

    PHP Code:
    switch ($variable) {
      case 
    1// Do Stuff Here
              
    break;
      case 
    2// Do Stuff Here
              
    break;
      case 
    3// Do Stuff Here
              
    break;
      case 
    4// Do Stuff Here
              
    break;
      case 
    5// Do Stuff Here
              
    break;
      case 
    6// Do Stuff Here
              
    break;
      default: 
    // Show Error
              
    break;

    You can even make sure it is an integer by sending the variable through intval() first, which is a good practice to get into.
    Wayne Luke
    ------------


  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Modesto, CA
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    switch

    In many occasions, you may want to compare the same variable (or expression) with many different values,
    Thats what is says in the PHP manuel, but I think I want to do it backwards, I want to compare the value = 0 to many variables? So do I still use the switch thing?

    [Edit]Sorry Im still new to php commands.
    Last edited by Imex; Jan 21, 2002 at 20:00.

  4. #4
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use the code wayne gave, and add a case *: where * is your value for everything you want to check

    Then, add a

    default:
    // do something here

    At the end.. as an error handle

    PHP Code:
    switch ( $var ) {
         case 
    0:
         
    // do something
         
    break;

         case 
    0:
         
    // do something
         
    break;

         case 
    0:
         
    // do something
         
    break;

         case 
    0:
         
    // do something
         
    break;

         default:
         
    // error here

    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us


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
  •