SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot amit290's Avatar
    Join Date
    Oct 2001
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Basic PHP Question: if / elseif / else

    Right I was just wondering which way is right or best.

    I've got 3 variables and depending on which one is submitted via a form I want some code to be executed.

    • add
    • edit
    • delete
    CODE 1
    PHP Code:
    if ($add == "1") {
        
    //do something
    }
    if (
    $edit == "1") {
        
    //do something
    }
    if (
    $delete == "1") {
        
    //do something
    }
    else {
        
    //just do this

    OR

    CODE 2
    PHP Code:
    if ($add == "1") {
        
    //do something
    }
    elseif (
    $edit == "1") {
        
    //do something
    }
    elseif (
    $delete == "1") {
        
    //do something
    }
    else {
        
    //just do this

    I looked at some old code and it was example 2. I've been writing some new code which is like example 1. Is one way right and the other wrong? or any fault with either.

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    I don't know if there is any right or wrong here. I don't think so.

    If only one of the variables can be set at the same time I would go with CODE 2. It just seems more logical to me, because they have been sent from the same form and you are going to decide which one of them who is set.

    Someone will probably pop in and tell you the directly opposite though...

    -Helge

  3. #3
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I'd do is different, say the URL is: page.php?action=edit (or add,delete) and use switch ($action). It's cleaner, safer and easier too.

    Code:
    switch ($_POST['action']) {
     case 'add': add(); break;
     case 'edit': edit(); break;
     case 'delete': delete(); break;
     default: printUsage();
    }
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  4. #4
    SitePoint Zealot amit290's Avatar
    Join Date
    Oct 2001
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Silly question maybe, but why is it safer?

  5. #5
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another alternative is:
    PHP Code:
    if(isset($_POST['action']) && function_exists($_POST['action'])){
        
    $_POST['action']();

    This gives you the advantage of being able to add new functions without having to edit/maintain a large switch statement. Course, you'll want to check that action=exec etc isn't posted , so maybe have an array of allowed functions or a file_exists() if say the code for edit is in edit.php.

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Son Nguyen
    It's cleaner, safer and easier too
    Cleaner and easier is a subjectiv opinion, so I won't ask about those, but out of curiousity: How is it safer than an if statement? I've never heard that before so I'm just curious. Maybe I've to change coding habit.

    -Helge

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow, three within the same minute.

    -Helge

  8. #8
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It can't be safer, but cleaner, since the switch was made for situations like this one.

  9. #9
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it's safer compared to the original code in a sense that $add=="1", a specific value and is not intuitive. The fact that it's cleaner makes it safer too (so you won't miss this/that => bugs ). Anyway, just my coding guidelines
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


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
  •