[UPHPU] More if then fun...

District Webmaster webmaster at alpine.k12.ut.us
Wed Apr 28 12:20:33 MDT 2004

The problem is with your operator precedence.

Logical operators in php are evaluated from left to right. In your
second if statement, you're asking php to evaluate
  if ($k == "Website" || $k == "Email_Address" && $text !="")
the first thing it evaluates is the first logical operator from the left
(||), which is to say it evaluates the two conditions, from left to
right, on both sides of the ||

so the first thing it asks is if $k is equal to "Website" (which it is)

since || evaluates to true if _either_ side of || is true, it does not
matter whether the rest of your conditional statement is true or not --
this statement evaluates to true, so the conditional code is executed.

in your first if statement ($k == "Websites" || $k == "Email_Address")
evaluates as true, so the conditional code within it is executed -- but
that code includes another conditional that evaluates if ($text !="") --
which it is not, so its conditional code is not executed.

Hope that's not to confusing,


>>> "Ashley Oviatt" <ashovi at qwest.net> 04/28/04 12:04 PM >>>
Can someone explain to me why the first if statement works (doesn't 
produce the output,) and the second one doesn't work (Does produce the 

I'm assuming the syntax of the second if statement is wrong, but I don't

know why.

	$text = "";	
	$k = "Website";

// First if statement
	if ($k == "Websites" || $k == "Email_Address")
         if ($text !="")
		print "hello I am the first test";

//Second if statement.
	if ($k == "Website" || $k == "Email_Address" && $text !="")

		print "hello I am the second test";

The output of the code is : hello I am the second test


To unsubscribe, e-mail: list-unsubscribe at uphpu.org
For additional commands, e-mail: list-help at uphpu.org

More information about the UPHPU mailing list