5 Replies - 3030 Views - Last Post: 08 May 2012 - 08:39 AM Rate Topic: -----

#1 bythegram  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-May 12

If/Elseif statements not working properly

Posted 08 May 2012 - 07:56 AM

Hi all,

I am quite new to php and am stuck with some form code. I need it to generate 1 of 3 possible outcomes depending on the answers. I have got the form working to the point that it creates variables as integers because I figured it would be easier to create the "if/elseif" statements that way.

Here is my "if/elseif" statements:

if(hasError == true && $a== 1 && $b==1 && $c==1 && d==1 && $e == 1 && $f == 1 && $g == 1) { //this if statement is not working!
					$args = "bucket1";
					$emailSent = true;
		}
		elseif(hasError == true && $a== 2 && $b==2  && $c==2 && d==2 && $e == 2 && $f == 2 && $g == 2) { //this if statement is not working properly!
					$args = "bucket2";
					$emailSent = true;
		}
		elseif(hasError == true && $a== 1 || 2 && $b==3 && $c==3 || 4 && d== 3 && $e == 1 || 2 && $f == 1 || 2 && $g == 3 || 4) { //this if statement is not working properly!
					$args = "bucket3";
					$emailSent = true;
		}
		
	}



To keep things simple I'm testing the statement by just displaying the $args variable later on. No matter what answers are put it always results in the last elseif statement being true.

I hope that made sense, the problem just seems to simple and I feel I am going about it the wrong way.

Um FYI I just realized that I messed up the "hasError" variable and is supposed to == false. This does not fix the issue though.

Is This A Good Question/Topic? 0
  • +

Replies To: If/Elseif statements not working properly

#2 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,303
  • Joined: 19-March 11

Re: If/Elseif statements not working properly

Posted 08 May 2012 - 08:06 AM

if(hasError == true && $a== 1 && $b==1 && $c==1 && d==1 && $e == 1 && $f == 1 && $g == 1) { //this if statement is not working!


I expect the statement is doing exactly what you told it to - I'd find it quite remarkable if you'd found a bug in something so basic, even in PHP. So what does this do? It returns true just in case each of its component expressions returns true. Check to see which one is not returning true and you'll have an idea of where to start debugging.

This post has been edited by jon.kiparsky: 08 May 2012 - 08:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,058
  • Joined: 08-June 10

Re: If/Elseif statements not working properly

Posted 08 May 2012 - 08:12 AM

do you really have a constant named d?

what is $a== 1 || 2 supposed to mean? (it always evaluates as true*)




* - in case you can’t follow: the expression is equivalent to ($a== 1) || (2). if the first one is true, it yields true. if the first one is false, it also yields true, because 2 evaluates as true (false || 2 => false || 2 = 2 = true)

This post has been edited by Dormilich: 08 May 2012 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,303
  • Joined: 19-March 11

Re: If/Elseif statements not working properly

Posted 08 May 2012 - 08:14 AM

View PostDormilich, on 08 May 2012 - 10:12 AM, said:

do you really have a constant named d?



Ooh, good catch.
Was This Post Helpful? 0
  • +
  • -

#5 bythegram  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-May 12

Re: If/Elseif statements not working properly

Posted 08 May 2012 - 08:23 AM

Ah good catch on the d variable. No it is supposed to be $d and your point about the last condition makes sense. I can't remember the original reasoning behind it. I'm going to try somethings out.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 bythegram  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-May 12

Re: If/Elseif statements not working properly

Posted 08 May 2012 - 08:39 AM

FYI the major problem were that I messed up in calling the correct variables.

The form is now working fine. Thanks all.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1