Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [Solved] Switch or if()?

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #28615
    Irrorate
    Member

    This is a question that I’ve pondered for a while. Which is more code-efficient? Which is more performance-efficient? Will a switch statement run quicker than many different if statements?

    Take this for example:

    Code:
    if($_GET[‘do’] == ‘add’){
    // code here
    } elseif { ($_GET[‘do’] == ‘edit’){
    // code here
    } elseif { $_GET[‘do’] == ‘delete'{
    // code here
    } else {
    // code here
    }

    Would the above if statement run better than:

    Code:
    switch($_GET[‘do’]){
    case(‘add’):
    // code here
    break;
    case(‘edit’):
    // code here
    break;
    case(‘delete’):
    // code here
    break;
    }

    Thanks for any opinions.

    #73388
    Rob MacKay
    Participant

    yea I agree with Mark – :)

    #73389
    Irrorate
    Member

    Thanks guys, I had a feeling a switch would run better than an if/elseif/else.

    Thanks for clarifying, just trying to make my code more efficient! :)

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.