Click to See Complete Forum and Search --> : Function cannot execute


jamesm6162
01-05-2008, 11:49 AM
Hi

I wrote the following script, but for some reason the function refuses to execute when it's called. The only output I receive is the "---" assigned to the variable at the start.

What am I doing wrong?


$html="---";
function checkSession()
{
$html.="checksession started";
if (session_id() != "")
{
$html .= "Session in Progress";
}
elseif (isset($_POST["username"]))
{
$html .= "Logging in";
}
else {$html .= "Not logged in";}
}
checkSession();
echo $html;

scragar
01-05-2008, 11:58 AM
$html="---";
function checkSession()
{
global $html; // magic line.
$html.="checksession started";
if (session_id() != "")
{
$html .= "Session in Progress";
}
elseif (isset($_POST["username"]))
{
$html .= "Logging in";
}
else {$html .= "Not logged in";}
}
checkSession();
echo $html;

NogDog
01-05-2008, 09:14 PM
Or, to make the function more portable and re-usable, use a function parameter instead of global. (It also makes your code easier to maintain and less prone to the pesky sorts of bugs that can arise over time due to the tight coupling caused by the use of globals.)

$html="---";
function checkSession($html)
{
$html.="checksession started";
if (session_id() != "")
{
$html .= "Session in Progress";
}
elseif (isset($_POST["username"]))
{
$html .= "Logging in";
}
else {$html .= "Not logged in";}
}
$html = checkSession($html);
echo $html;

jamesm6162
01-06-2008, 04:16 AM
Yeah that solved it.

Thanks so much for the help.