conditioned inclusion

Discussion in 'PHP' started by canishk, Mar 12, 2009.

  1. #1
    Hello,

    I need to know whether conditional inclusion is possible in php. If not can anyone suggest a better method to so.

    This is what I am trying to do:
    
    if($page == "user_list")
    	include("includes/user_list.php");
    elseif($page == "adduser")
    	include("includes/user.php");
    
    PHP:
    Please help me, thanks in advance.
     
    canishk, Mar 12, 2009 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    yes its possible, your in the right track, your code is good..
    conditional statements for conditional method (if, else, elseif, do, while, foreach, switch, etc..)
     
    bartolay13, Mar 12, 2009 IP
  3. Beaconwebsol

    Beaconwebsol Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah the way you are suing code is right. There's nothing wrong in it. Keep your code more towards OO.
     
    Beaconwebsol, Mar 12, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That code works. I sometimes don't understand why people ask questions like that rather than just trying it, which is faster than posting the question in a forum.
     
    SmallPotatoes, Mar 12, 2009 IP
  5. canishk

    canishk Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    got the code.

    Thank You all.

    
    <?php
    
    // This is WRONG and will not work as desired.
    if ($condition)
        include $file;
    else
        include $other;
    
    
    // This is CORRECT.
    if ($condition) {
        include $file;
    } else {
        include $other;
    }
    
    ?> 
    
    PHP:
     
    canishk, Mar 12, 2009 IP
  6. canishk

    canishk Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It is only because that code didn't work.

    Check the above code for the difference.

    It is better try before you comment something.

    anyway

    thank You.
     
    canishk, Mar 12, 2009 IP
  7. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #7
    it is both correct, theres just no ':'
    try this one its the same

    if ($condition):
    include $file;
    elseif ($bar):
    include $other;
    endif;
     
    bartolay13, Mar 13, 2009 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The two different code samples you posted have identical effect. If you are getting different results then something else is going on.
     
    SmallPotatoes, Mar 13, 2009 IP
  9. canishk

    canishk Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Well may be, but digital point is my last resort for clearing my problems.

    Thank you all for helping me out.
     
    canishk, Mar 16, 2009 IP