Results 1 to 5 of 5

Thread: if statements and functions

  1. #1
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default if statements and functions

    Hi is it possible to have an if statement and then have functions.

    I know i am unclear so this is what i want to happen.

    PHP Code:
    if($_SESSION['loggedin'] == true){
    function 
    ()
        }
        else
        {
    function 
    2()   
        } 
    If it isnt possible how can i get around this.
    Last edited by simsonite; 12-13-2008 at 10:55 AM.

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I'm not sure. Why don't you just test it out?

    Also, there isn't any reason that I am aware of to not just load up all the functions you may or may not use on a page.

    What are you trying to accomplish by doing what you have explained?

    J

  3. #3
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Ok here is the actual code

    PHP Code:
    <?php
    include ('function.login.php');
    if(
    $_SESSION['loggedin'] == true)
        {
        function 
    loginForm ()
        }
        else
        {
        echo 
    "testing"
        
    }
    ?>
    The problem is that it comes up with the following error
    "
    Parse error: syntax error, unexpected '}', expecting '{' in /home/-----/public_html/---/login.php on line 6
    "

    Also sorry for not explaining properly before.

  4. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Code:
    <?php
    
    include ('function.login.php');
    
    if ( $_SESSION['loggedin'] == true ) {
    
        function loginForm ();
    
    } else {
    
        echo "testing";
    
    }
    
    ?>
    Don't forget the semi colons after each statement.

    Inside of your if/else statement, do you wish to define the function loginForm or execute it. Normally, all of your function definitions are written prior to executing a script. I can't think of anytime you would define a function inside of an if/else statement.

    So it would make more sense to have something like this:

    Code:
    <?php
    
    // define the function loginForm
    function loginForm () {
       
       function code here...
    
    }
    
    include ('function.login.php');
    
    if ( $_SESSION['loggedin'] == true ) {
    
       // execute the function loginForm
        loginForm ();
    
    } else {
    
        echo "testing";
    
    }
    
    ?>
    I hope this helps. If not, try explaining exactly what the goal of your script is.

    Good luck!

    Jason

  5. The Following User Says Thank You to JasonDFR For This Useful Post:

    simsonite (12-13-2008)

  6. #5
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Lol now i feel dumn =)

    Thanks i cant believe i forgot the semi colons and i wanted to execute the functions but i have only just started learning functions and classes.

    Thanks for your help im sure i will be back again =)

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
  •