# Anything wrong with this if statement?

**URL:** <https://forum.kirupa.com/t/anything-wrong-with-this-if-statement/259871>\
**Category:** Uncategorized\
**Created:** [May 9, 2008, 3:31pm UTC](https://forum.kirupa.com/t/anything-wrong-with-this-if-statement/259871 "2008-05-09T15:31:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patch](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/patch/32/1095_2.png) [@Patch](https://forum.kirupa.com/u/Patch)\
**Post date:** [May 9, 2008, 3:31pm UTC](https://forum.kirupa.com/t/anything-wrong-with-this-if-statement/259871/1 "2008-05-09T15:31:18Z")

</div>

I’m trying to make a password update script. The SQL works fine, but the validation to make sure that the fields are not left empty and that they both match. Doesn’t seem to work. It just seems to run the first part of the if statement (Password Changed!) bit etc…

Any ideas?

```php

if(isset($_POST['pass_submit']))
					
					{
					
					if(($pass !== "") || ($con_pass !== "") || ($pass == $con_pass))
						
						{	
							$sql = "UPDATE users SET password = PASSWORD('$pass') WHERE id = '$u_id'";
							$result = mysql_query($sql) or die(mysql_error());
							
							$msg = '<p align="center">Password Changed!</p>';
							$_SESSION['msg'] = $msg;
							header("Location: #pref");
								
						}else{
							
							$msg = '<p align="center">Sorry an error occured. Make sure your password entries match up and the fields are not left blank. </p>';
							$_SESSION['msg'] = $msg;
							header("Location: #pref");
							exit;
						
						}
						
					}

```

Cheers

---

<div class="post-metadata">

**Author:** ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)\
**Post date:** [May 9, 2008, 3:56pm UTC](https://forum.kirupa.com/t/anything-wrong-with-this-if-statement/259871/2 "2008-05-09T15:56:57Z")

</div>

```php

if(($pass != "") && ($con_pass != "") || ($pass == $con_pass)) 

```

---

<div class="post-metadata">

**Author:** ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)\
**Post date:** [May 9, 2008, 4:06pm UTC](https://forum.kirupa.com/t/anything-wrong-with-this-if-statement/259871/3 "2008-05-09T16:06:31Z")

</div>

ooooohhhh 😮 OMG OMG OMG! What simp said is ambiguous. He correct, but the if statement conditions are ambiguous.

You’d want to do

```php

if ( ( ($pass != "") && ($con_pass != "" ) ) || ($pass == $con_pass) )

```

I don’t know how to explain this very well, but you don’t want to have && and || on the same level.  
Like, if ( condition1 && condition2 || condition3 )  
It’s confusing, because it may do something different from what you expect it to do. Do you think it’ll do condition1 && condition2 first, and then take that result and do || with condition3 or something else?

(I know there’s operator precedence, but putting ( ) is better). 🙂

---

<div class="post-metadata">

**Author:** ![Patch](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/patch/32/1095_2.png) [@Patch](https://forum.kirupa.com/u/Patch)\
**Post date:** [May 9, 2008, 4:26pm UTC](https://forum.kirupa.com/t/anything-wrong-with-this-if-statement/259871/4 "2008-05-09T16:26:54Z")

</div>

thanks guys, but neither of them work :’(

---

<div class="post-metadata">

**Author:** ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)\
**Post date:** [May 9, 2008, 8:40pm UTC](https://forum.kirupa.com/t/anything-wrong-with-this-if-statement/259871/5 "2008-05-09T20:40:12Z")

</div>

What part doesn’t work (and you want to make it work)?

---

<div class="post-metadata">

**Author:** ![Patch](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/patch/32/1095_2.png) [@Patch](https://forum.kirupa.com/u/Patch)\
**Post date:** [May 9, 2008, 10:38pm UTC](https://forum.kirupa.com/t/anything-wrong-with-this-if-statement/259871/6 "2008-05-09T22:38:21Z")

</div>

yes I do want to make it work lol, as in function, operate and perform correctly. It just seems to be doing the same as it did before. it changes the password regardless if the fields are blank and do not match up.

Where as I need it to check first if the correct parameters are in place i.e. both fields match and are not left blank.

any suggestions?

Thank you.

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [May 13, 2008, 1:34pm UTC](https://forum.kirupa.com/t/anything-wrong-with-this-if-statement/259871/7 "2008-05-13T13:34:54Z")

</div>

> Where as I need it to check first if the correct parameters are in place i.e. both fields match and are not left blank.

That would be:

```php
if($pass != "" && $pass == $con_pass)

```

You don’t need to check if both fields are not blank as long as you are checking that they both match. For example, if $pass is not blank and $pass matches $con\_pass, there’s no way that $con\_pass would be blank.

I wanted to ask, though, in the snippet of code you provided, it doesn’t look like you are assigning the values to $pass and $con\_pass. PHP used to automatically create variables based on the post or get values, but newer versions have disabled that security risk. I like to use extract($\_POST); This creates variables from all of the key names in the $\_POST superglobal. For example if you have $\_POST[‘pass’], extract($\_POST); gives you $pass. The same goes for any other $\_POST values you have submitted.
