Page 1 of 1

If statement help [SOLVED]

Posted: Thu Aug 11, 2011 4:38 am
by 62896dude
Hey guys,

I can't quite seem to figure this out... Why is it always bypassing this if statement, even if I select one of the values that it says it can't select? (For example, if I select a spell with an id of 4, even though the If statement (alledgedly) says that if I select that, that part of the script should be skipped, it continues to carry out that part of the code anyways)

Here is the line that it is bypassing when it shouldn't:

Code: Select all

if ($spellfetch['id'] !== 1 || $spellfetch['id'] !== 4 || $spellfetch['id'] !== 7 || $spellfetch['id'] !== 9 || $spellfetch['id'] !== 11 || $spellfetch['id'] !== 13 || $spellfetch['id'] !== 15 || $spellfetch['id'] !==  16 || $spellfetch['id'] !== 17)
Do I need to switch the || to && ? I don't think I do, but who knows, nothing else seems to work :P

Thanks!

Re: If statement help

Posted: Thu Aug 11, 2011 6:01 am
by 62896dude
Nevermind everyone, there were so many things wrong with the code, I just started it over fresh, and it works fine now! (horray, I get to re-write 831 lines of code! :| )

Re: If statement help [SOLVED]

Posted: Thu Aug 11, 2011 6:27 am
by Jackolantern
Well, it is better than re-writing 10,000 lines further down the road ;)

However, there is a bit more elegant way of handling that same logic you listed above:

Code: Select all

$listVals = array(1, 4, 7, 9, 11, 13, 15, 16, 17);

if (!in_array($spellfetch['id'], $listVals)) {
    //run this code if $spellfetch['id'] is not one of the values in $listVals
}
That way you have your list of spell IDs to check against, and it will be much easier and less error-prone to change them or the way they are checked later :)

EDIT: Actually, wait, looking back at your code, that whole conditional statement basically would have short-circuited. Remember that || means OR, and once a single of the conditions results in TRUE, the rest of the logic condition statement is skipped and the results are assumed true. So it would basically always evaluate to TRUE, because right on the first condition, the parser would be thinking "Ok, so is the ID not 1?" and if it is not, then the whole thing is true. Even if it was 1, when it goes to the next part, the ID is obviously not also 4, so it would for sure be TRUE at that point.

I am assuming what you probably meant with that statement is to use && (AND). That would have the effect of reporting true only if the ID was not any of those numbers, and that is the problem that my code also solves.

Re: If statement help [SOLVED]

Posted: Thu Aug 11, 2011 6:42 am
by 62896dude
Yeah, thats what I figured out my second time around making that part :) And you are right, in the long run, 800 lines isn't too much of a toll to pay for a good old fashion lesson learned (the hard way) ;)