Page 1 of 1

|| or &&

Posted: Sat Aug 27, 2005 8:39 pm
by s.dot
Simple question.. I get confused quite easily :(

I'm not sure whether I need to use || or &&

Code: Select all

// page url would look something like domain.com/forumsearch.php?fts=All&keywords=keyword&author=author

// don't freak out =) the variables were already converted from $_GET['var'] to $var 

$sql = "SELECT topicid FROM forumentries WHERE";
	if((!$fts) || ($fts == "All"))
	{
		$sql .= " forumid IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)";
	} ELSE
	{
		$sql .= " forumid = '$fts'";
	}
	if(($keywords != "") || ($keywords != "keyword"))
	{
		$sql .= " AND entry LIKE '%$keywords%'";
	}
	if(($author != "") || ($author != "author"))
	{
		$sql .= " AND author LIKE '%$author%'";
	}
	$sql.= " GROUP BY topicid";
Basically in my IFs I need to see if the variable is set and it's not equal to the word in ""s
For some reason I get really confused when it comes to || or && :(

Posted: Sat Aug 27, 2005 9:08 pm
by nielsene
I'm not sure what your question is, please forgive me if what I say now is already obvious to you.

'&&'="And", just like in normal prose usage '&' = 'and' such as "Joe & I whent to the store."
If you're not used to that, you can try to remember '&'=ampersand, ampersand starts with 'a', so does 'and'

'||' = 'or' and its the "other" one, not and.

Posted: Sat Aug 27, 2005 9:13 pm
by s.dot
I know this :P

But it confuses me, like when I say....... this || this... does this mean both conditions are met, or one or the other?

lets say I wanted $var to have a value, and for that value not to be equal to "one"

would it be

if($var != "" || $var != "one")

or

if($var !-"" && $var != "one")

*sigh* I confuse myself

Posted: Sat Aug 27, 2005 9:16 pm
by feyd
or - the left or right expression must evaluate to true/non-zero. Parsed left to right.
and - both left and right expressions must evaluate to true/non-zero. Parsed left to right.
xor - either left or right expression must evaluate to true/non-zero (they must be different). Parsed left to right.

Posted: Sat Aug 27, 2005 9:21 pm
by nielsene
Ahh, yes as feyd says, put the conditions in the normal reading order.

Ie a somewhat common sequece is

Code: Select all

if (isset($_GET["foo") && $_GET["foo"]=="bar") {
}
as one way of avoiding the "Undefined index" error message. Both Or and And are "short-circuited", as soon as PHP "knows" the value of the expression it stops. "True Or anything" is always true. "False And anything" is always false. so you can put "guard" conditions first.

Posted: Sat Aug 27, 2005 9:59 pm
by s.dot
Okay so is this code correct

Code: Select all

$sql = "SELECT topicid FROM forumentries WHERE";
if(($fts == "") || ($fts == "All"))
{
	$sql .= " forumid IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)";
} ELSE
{
	$sql .= " forumid = '$fts'";
}
if(($keywords != "") && ($keywords != "keyword"))
{
	$sql .= " AND entry LIKE '%$keywords%'";
}
if(($author != "") && ($author != "author"))
{
	$sql .= " AND author LIKE '%$author%'";
}
$sql.= " GROUP BY topicid";
This is what I want it to do, in English

If $fts is empty or $fts is equal to "All" then $sql should .= forumid IN (...)
otherwise, it should be forumid = '$fts'

If $keywords is not empty and $keywords is not equal to "keyword"
then add this to the sql - " AND entry LIKE '%keyword%'

If $author is not empty and $author not equal to "author"
then add this to the sql - " And author LIKE '%author%'
Is this what I have done with my code?

:?

Posted: Sat Aug 27, 2005 10:01 pm
by s.dot
bah, I've tried to wrap my brain around this concept enough, time for trial and error. I guess that's the only true way to understand it.

Posted: Sat Aug 27, 2005 10:01 pm
by nielsene
Yes, the code implements the textual description.

I'm torn on if some of those should be converted using DeMorgan's or not, but its correct in either case.

[Edit: Convinced myself that its better the way it is. So disregard my comment about DeMorgan's]

Posted: Sat Aug 27, 2005 10:02 pm
by s.dot
Thank you very much :-D

I feel such n00bish again.

Edit: Especcially after looking at your post above mine in the PHP-Code forum. I got $5 on that's not really PHP :lol: :P

Posted: Sun Aug 28, 2005 7:47 am
by jayshields
if it confuses you to read code like that just use "AND" and "OR" where you would use "&&" and "||"

Posted: Mon Aug 29, 2005 1:01 am
by n00b Saibot
You have done in code what you have written in text but I think you should check for both conditions to be true and hence && should be it...