Log in

View Full Version : Resolved if statements and functions



simsonite
12-12-2008, 05:36 PM
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.



if($_SESSION['loggedin'] == true){
function 1 ()
}
else
{
function 2()
}


If it isnt possible how can i get around this.

JasonDFR
12-12-2008, 06:01 PM
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

simsonite
12-12-2008, 07:08 PM
Ok here is the actual 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.

JasonDFR
12-12-2008, 09:39 PM
<?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:


<?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

simsonite
12-12-2008, 10:19 PM
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 =)