Re: order of evaluation
| From: | Erik Price | Date: | Tue, 09 Apr 2002 19:13:13 +0000 |
| Subject: | Re: order of evaluation | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-92286@lists.php.net to get a copy of this message | ||
Hey I'm sorry Dan and everyone, I realize my mistake -- if you're curious it's in my if/elseif/else chain, I explain the problem below (scroll down):
Erik
On Tuesday, April 9, 2002, at 03:00 PM, Erik Price wrote:
Hm. I threw in a test echo statement in the class method, to make sure that it wasn't being accessed if $_POST['newpassword'] is in fact empty. The test echo statement is displaying on the receiving page, however, even though there is nothing in $_POST['newpassword']. Here's my class method: """ function set_password($password) { echo "<p class=\"warning\">set_password() accessed, the new password is '$this->password'</p>"; // remove after bug testingWhat happened was the "elseif" test I was performing was checking to see if either one of two conditions was true: 1st condition: newpassword is empty, and newpassword does not equal confirmpassword 2nd condition: set_password() fails In my case, both newpassword and confirmpassword were empty strings, so the first condition evaluated to true. This forced the parser to evaluate the second part of the expression (after the ||), which accessed the set_password() method after all. By adding a second "!empty($_POST['newpassword'])" to the second half of the expression (after the ||), I solved my problem.if (!preg_match('/\d/', $password) || !preg_match('/^[-A-Za-z0-9!@#$%^&*()_ +=\?]{6,10}$/', $password) || !preg_match('/[A-Za-z]/', $password)) {return false;} else { $this->password = $password; return true; }} """ and here's the code that's hitting the method (but shouldn't be): """ // the Person class is needed here require_once('./includes/Person_class.inc'); // create a new Person instance $user = new Person(); if (!$user->set_email($_POST['email'])) { $error_message = "invalid email"; } elseif ( (!empty($_POST['newpassword']) && $_POST['newpassword']!= $_POST['confirmpassword']) ||(!$user->set_password($_POST['newpassword'])) ) {
$error_message = "<p class=\"warning\"></p>\n"; } else { $success_message = changeinfo_process(serialize($user)); } if ($error_message) { return $error_message; } elseif ($success_message) { return $success_message; } else { die("No error or success message?"); } // destroy the Person instance unset($user); """ On the next page, the echo from my class method is appearing: """ set_password() accessed, the new password is '' """ But as you can see, the value of $_POST['newpassword'] is an empty string -- shouldn't the elseif statement testing for !empty($_POST['newpassword'] catch this? Much thanks to anyone with clearer thinking than I. Erik ---- Erik Price Web Developer Temp Media Lab, H.H. Brown pricee@hhbrown.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php---- Erik Price Web Developer Temp Media Lab, H.H. Brown pricee@hhbrown.com