Jump to content

IF OR Help


herghost

Recommended Posts

Good evening everyone,

 

I am having some problems with the correct method of writing a piece of code, basically what I have is this:

if(!empty($_SESSION['reg'])) || 
	(!empty($_SESSION['fail']))|| 
	(!empty($_SESSION['logged'])) ||
	(!empty($_SESSION['failedlog']))
	{
		echo '<div id="alert">' . $_SESSION['reg'] . '</div>';
		echo '<div id="alertr">' . $_SESSION['fail'] . '</div>';
		echo '<div id="alert">' . $_SESSION['logged'] . '</div>';
		echo '<div id="alertr">' . $_SESSION['failedlog'] . '</div>';
		unset($_SESSION['reg']);
		unset($_SESSION['fail']);
		unset($_SESSION['logged']);
		unset($_SESSION['failedlog']);

	}

 

Which should basically fire off the div alert or alertr on page load if one of the sessions exists. Only one of these sessions will ever exist at anyone time.

 

However this gives me an parse error on the 1st line. Session_start(); is set early in the script and all sessions echo out if i printr them before the unset.

 

Can anyone point me in the right direction of how something like this should be formatted?

 

Many Thanks

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/217590-if-or-help/
Share on other sites

Your parentheses are borked.  You can use this:

if(!empty($_SESSION['reg']) || 
!empty($_SESSION['fail']) || 
!empty($_SESSION['logged']) ||
!empty($_SESSION['failedlog']))
{

 

Or if you are actually checking for isset, then this:

if(isset($_SESSION['reg'], $_SESSION['fail'], $_SESSION['logged'], $_SESSION['failedlog'])) {

Link to comment
https://forums.phpfreaks.com/topic/217590-if-or-help/#findComment-1129610
Share on other sites

Or if you are actually checking for isset, then this:

if(isset($_SESSION['reg'], $_SESSION['fail'], $_SESSION['logged'], $_SESSION['failedlog'])) {

 

I agree that isset() is probably the best way to check this, but adding all the vaues into one isset() will not give the intended results. When adding multiple values in isset, it will return TRUE only when all the values are true. In this case the OP wants to return true if one of the values isset.

 

So, instead you could use:

if(isset($_SESSION['reg']) || isset($_SESSION['fail']) || isset($_SESSION['logged']) || isset($_SESSION['failedlog'])) {

 

However, if empty() is the correct test in this situation, then you could simplify the condition by concatenating the values as follows:

if(!empty($_SESSION['reg'].$_SESSION['fail'].$_SESSION['logged'].$_SESSION['failedlog'])) {

Link to comment
https://forums.phpfreaks.com/topic/217590-if-or-help/#findComment-1129628
Share on other sites

Thanks to both of you, success! I used this in total:

 

if(isset($_SESSION['reg']) || isset($_SESSION['fail']) || isset($_SESSION['logged']) || isset($_SESSION['failedlog']) || isset($_SESSION['logout'])) 
	{
		if(isset($_SESSION['reg'])){
			echo '<div id="alert">' . $_SESSION['reg'] . '</div>';
			unset($_SESSION['reg']);
		}
		if(isset($_SESSION['fail'])){
		echo '<div id="alertr">' . $_SESSION['fail'] . '</div>';
		unset($_SESSION['fail']);
		}
		if(isset($_SESSION['logged'])){
		echo '<div id="alert">' . $_SESSION['logged'] . '</div>';
		unset($_SESSION['logged']);
		}
		if(isset($_SESSION['failedlogged'])){
		echo '<div id="alertr">' . $_SESSION['failedlog'] . '</div>';
		unset($_SESSION['failedlog']);
		}
		if(isset($_SESSION['logout'])){
		echo '<div id="alertr">' . $_SESSION['logout'] . '</div>';
		unset($_SESSION['logout']);
		session_destroy();
		}
	}


?>

Link to comment
https://forums.phpfreaks.com/topic/217590-if-or-help/#findComment-1129635
Share on other sites

Then why do you need the first IF statement?

 

You could get the same results with the following:

$sessionIndexes = array(
    'reg'       => 'alert',
    'fail'      => 'alertr',
    'logged'    => 'alert',
    'failedlog' => 'alertr',
    'logout'    => 'alertr'
);
foreach($sessionIndexes as $index => $divID)
{
    if(isset($_SESSION[$index]))
    {
        echo "<div id=\"{$divID}\">{$_SESSION[$index]}</div>";
        unset($_SESSION[$index]);
        session_destroy();
    }
}

 

Link to comment
https://forums.phpfreaks.com/topic/217590-if-or-help/#findComment-1129649
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.