If statement help [SOLVED]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

If statement help [SOLVED]

Post 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!
Last edited by 62896dude on Thu Aug 11, 2011 6:01 am, edited 1 time in total.
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: If statement help

Post 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! :| )
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: If statement help [SOLVED]

Post 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.
The indelible lord of tl;dr
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: If statement help [SOLVED]

Post 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) ;)
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
Post Reply

Return to “Beginner Help and Support”