Jump to content

Variable not defined problem.


sepoto

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

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!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...