www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Short-hand for IF($var == "value" || $var == "value2")

  1. #1
    Join Date
    Mar 2006
    Location
    Colorado
    Posts
    61

    resolved [RESOLVED] Short-hand for IF($var == "value" || $var == "value2")

    I'm curious if there's a shorter way of writing this.. i've come across situations like this a few times where i don't know what best practice is or if there's a quicker way..

    PHP Code:
    if($page=="home" || $page=="about" || $page=="contact" || $page=="course" || $page=="enroll" || $page=="login") { 
    Basically it's just checking to see what page it is.. perhaps it would be faster to do with a switch statement. I'm sure a well-polished PHP developer can tell me Thanks!

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    Use switch(code is much faster).
    PHP Code:
    switch($page){
    case 
    "home":
    case 
    "about":
    case 
    "contact":
    case 
    "course":
    case 
    "enroll":
    case 
    "login":
      
    ///......
    break;
    default:
     
    // else stuff here :p
    break;

    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  3. #3
    Join Date
    Nov 2008
    Posts
    2,477
    I tend to do something like:

    PHP Code:
    $check = array('home''about''contact''course''enroll''login');
    if (
    in_array($page$check)) {
    //...

    Useful if your list of pages is already in an array, or if you need to use them later for something else too.

  4. #4
    Join Date
    Mar 2006
    Location
    Colorado
    Posts
    61

    Awesome

    I think i'm gonna have to go with the in_array! Woot! I love learning new ways to speed up coding. Thank you much!
    My website: http://thinkclay.com
    My company's site: http://chosencreative.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles