|| or &&

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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

|| or &&

Post 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 && :(
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?

:?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Last edited by s.dot on Sat Aug 27, 2005 10:01 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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]
Last edited by nielsene on Sat Aug 27, 2005 10:06 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

if it confuses you to read code like that just use "AND" and "OR" where you would use "&&" and "||"
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

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