Jump to content

Variable not defined problem.


sepoto
 Share

Recommended Posts

In the following code I check to see if 'logged_in' is defined. The code works great but I keep getting an error that 'logged_in' is not defined yet which I am well aware of. I do not see the fact the 'logged_in' is not defined as an error myself. Is there a work around. I really do not like to see errors and warnings.

<?php	session_start();	if(!$_SESSION['logged_in']) {		echo "No user logged in.";	}	else {		echo "User logged in.";	}?>

Link to comment
Share on other sites

Try:if(!isset($_SESSION['logged_in']))Just using the ! operator tests the value. Since that session variable is not set, it will still throw the undefined error. The isset() function checks to see if a variable exists. You could also use the empty() function which combines the functionality of !isset(). Something like:if (empty($_SESSION['logged-in']))That checks if the variable is set, and whether or not it's value is "empty" (ie, it evaluates to false)EDIT: Shucks! I was too slow...

Edited by ShadowMage
Link to comment
Share on other sites

It seems like the correct approach, but PHP is picky. Use the isset or empty functions instead; usually, one makes more semantic sense than the other:

if (empty($_SESSION['logged_in']) ) {		echo "No user logged in.";}// ORif (!isset($_SESSION['logged_in']) ) {		echo "No user logged in.";}// OR any negation of those with the opposite consequence

Keep in mind that the variable can technically be set and also empty. I imagine that this will not be the case in your technique.EDIT. Man, I didn't even see you guys around!

Edited by Deirdre's Dad
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...