OR operator in if statements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marvolo1300
Forum Newbie
Posts: 9
Joined: Thu Aug 05, 2010 11:48 am

OR operator in if statements

Post by marvolo1300 »

Hi, could someone tell me if used the right syntax for the if (x||y) statement?

This is the error
unexpected T_BOOLEAN_OR on line 2

Code: Select all


if (!isset($_COOKIE['login']))||($_COOKIE['login'] != $hash) // line 1
{
	echo '<META HTTP-EQUIV="Refresh" Content="0; URL=index.php">';    
}

User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: OR operator in if statements

Post by PHPHorizons »

Hello marvolo1300,

You're close, but you closed the if block "conditional" immediately after the !isset() part. Moving that closing parenthesis to the end of that line will correct the problem:

Code: Select all

if (!isset($_COOKIE['login']) || ($_COOKIE['login'] != $hash)) // line 1
{
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=index.php">';    
}
Cheers
marvolo1300
Forum Newbie
Posts: 9
Joined: Thu Aug 05, 2010 11:48 am

Re: OR operator in if statements

Post by marvolo1300 »

Thanks it worked. I also figured out that i don't need to check if login cookie exists.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: OR operator in if statements

Post by PHPHorizons »

You're welcome

lol, that's ironic :P
Cheers
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: OR operator in if statements

Post by AbraCadaver »

marvolo1300 wrote:Thanks it worked. I also figured out that i don't need to check if login cookie exists.
If you're concerned about proper programming and not generating E_NOTICES for non-existent variables, then yes, you do need to check at some point before attempting to use/compare the variable.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply