if x != a OR b doesn't work

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
bladecatcher
Forum Commoner
Posts: 67
Joined: Sat Mar 12, 2005 12:50 am

if x != a OR b doesn't work

Post by bladecatcher »

G'day All,
I've searched everywhere I can think of but can't find out why this does not work. Any help appreciated.

This code works fine but when I combine them in a if statement using OR (see below) it fails can you explain why please?

this works:

Code: Select all

<?php
print $filename ;
if (file_exists($filename)) {
	print " File exists: " . $filename ;
	if ($filename != "contact.php") {
		print " NOT contact." ;
		if ($filename != "search.php") {
			print " NOT search." ;
			include "disclaimer.htm" ;
		} else {
			print " IS search." ;
		}
	} else {
		print " IS contact." ;
	}
}
?>
this doesn't work. why?

Code: Select all

<?php
print $filename ;
$contact = "contact.php" ;
$search = "search.php" ;
if (file_exists($filename)) {
	print " File exists: " . $filename ;
	if ($filename != $contact OR $search) {
		print " NOT contact or search." ;
		include "disclaimer.htm" ;
	} else {
		print " IS contact or search." ;
	}
}
?>
Thanking you in anticipation,
bladecatcher
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

your second variable ($search) is not getting checked against anything.

So this

Code: Select all

if ($filename != $contact OR $search) {
should actually be this

Code: Select all

if ($filename != $contact || $filename != $search) {
you could use || and OR. Both work the same way... Hope this helps.

could you possible post an error message?
bladecatcher
Forum Commoner
Posts: 67
Joined: Sat Mar 12, 2005 12:50 am

if x != a OR b doesn't work

Post by bladecatcher »

G'day,
WOW, this place IS busy.

Thank you very much for the instant reply.

still no go

Code: Select all

if ($filename != $contact || $filename != $search) {
does not work
but

Code: Select all

if ($filename != $contact AND $filename != $search)
does! Can someone explain why? Please

tia
bladecatcher
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Perhaps it is an error in your logic?

You should also use && instead of AND aswell..
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

just went ahead and deleted my failure of an attempt to explain that. someone with a better descriptive mind should tackle that one. too early in the morning and i'm about to pass out at the keyboard as it is lol...

quick note though.. && and AND are both ok and work the same. i guess it's just according to whatever you are most comfortable with...

anyways, GNITE 8O



edit: as feyd has pointed out, i am wrong (but not real suprise here...)

http://us2.php.net/if <-- the bottom statement left me thinking that what i had originally said was true, and i didn't actually do any research on the issue. i guess i also missed the very top message warning of using the "and" and "or" statements lol..

in other words, when ur mind turns blank, get some sleep.
Last edited by infolock on Sat Mar 12, 2005 3:54 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there is a difference between || and OR, && and AND .. it's called precedence: http://php.net/language.operators#langu ... precedence
bladecatcher
Forum Commoner
Posts: 67
Joined: Sat Mar 12, 2005 12:50 am

Thank you all very much

Post by bladecatcher »

G'day again,
I've eaten now and brain has been nourished, I see all.

Well, my mistake anyway. If it was an "=" and not "!=" the "OR" would have been correct, I think "+" and "-" terminals on my brain got crossed, lol. I'd previously read the manual on precedence and come to the conclusion the "OR" and "AND" were appropriate here, the bars and ampersands would be appropriate where "precedence" is required, correct?

Now I want to read a txt file of pages into an array and parse that so I'd better get started, THANKS again.

cheers,
bladecatcher
bladecatcher
Forum Commoner
Posts: 67
Joined: Sat Mar 12, 2005 12:50 am

PHP tags

Post by bladecatcher »

BTW,
first time I viewed the post my php tags didn't appear to work.

This is a test, please ignore.

Code: Select all

if (file_exists($filename)) {
//	if ($filename != $contact || $filename != $search) {
	if ($filename != $contact AND $filename != $search) { 
		include "disclaimer.htm" ;
	}
}
blade
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP tags

Post by Weirdan »

bladecatcher wrote:BTW,
first time I viewed the post my php tags didn't appear to work.
PHP tags mod has been redone recently. You might have been viewing the topic while old posts had been fixed.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

re: ^ and ^^ ..

some recently older browsers and current browsers (depeding on operating system and such) may have an initial issue due to the speed, or lack thereof, of the data coming across. For instance with the old code, it happened quite frequently that Firefox would render the page before it processed the highlighting. Thus making the display a few pixels high. A quick page refresh always seemed to fix the problem. I personally haven't had any issue with this in the new code with Firefox 1.0.1 (Win32). I know I used to with Firefox 0.8 - 0.9 (Win32)
Post Reply