dylpotato Posted July 10, 2010 Share Posted July 10, 2010 For some reason I cannot get the is_numeric() function to work. No matter what I pass in to the function (int or char) it returns true. I've used it many times before, so it must be something simple that I am not seeing. I could use an extra pair of eyes, please take a look and let me know if you see what is wrong. $error = false; $error_message = ""; $newpin = 2222; echo "pin:".$newpin . "<br/>"; // this prints "pin:2222"; // this function gets the user's pin from the database // returns 0 if there is no pin set if (getRealPin()) { $error = true; $error_message = "You already have a pin. You can reset your pin". " via <a href=\"userprefs.php\">user prefs</a>. "; } else if (!$newpin || $newpin = '') { $error = true; $error_message = "You did not enter a pin! Please go back and". " set a 4 digit pin number."; // this is where it is failing, I always get the error message here. } else if (!is_numeric($newpin)) { $error = true; $error_message = "Your pin number may only contain numbers. <br/>". "Please go back and try again."; } else if ($newpin < 1000 || $newpin > 9999) { $error = true; $error_message = "You entered an invalid pin! <br/>Please ensure that". " the pin you entered is four digits long and does ". "not start with a zero."; } if ($error) { echo $error_message; } else { // set the pin echo "Pin set."; } Any help would be great, -Dylan Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 10, 2010 Share Posted July 10, 2010 In this "if" statement <?php else if (!$newpin || $newpin = '') ?> you're using a single "=" instead of a double "==". The single "=" is for assignment, the double "==" is for comparison, so you're wiping out the value of $newpin. Ken Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 10, 2010 Share Posted July 10, 2010 In this line } else if (!$newpin || $newpin = '') { you're using = instead of == so efectively you're putting an ampty string into $newpin; I hate you Ken Quote Link to comment Share on other sites More sharing options...
BizLab Posted July 10, 2010 Share Posted July 10, 2010 Here you go homie.. it seems like it wont work with the "!" negation factor just integrate this logic structure into your code. I included the "pins" in an array to give more than one sample at a time. Just remove the foreach loop stuff to use the code $error = false; $error_message = ""; $newpin = array("2222","why didnt this work?", "089678"); foreach($newpin as $test){ echo "pin: ".$test . "<br/>"; // this prints "pin:2222"; $error_message = ""; if(is_numeric($test)){ // if the pin is numeric, go ahead and do something with it. echo 'Good Pin'; } else{ // if its not set your error and show it $error = true; $error_message = "Your pin number may only contain numbers. <br/>". "Please go back and try again.<br />"; } if ($error) { echo $error_message.'<br />'; } else { // set the pin echo "<br />Pin set.<br /><br />"; } } THe above code prints: pin: 2222 Good Pin Pin set. pin: why didnt this work? Your pin number may only contain numbers. Please go back and try again. pin: 089678 Good Pin let me know if that helps Quote Link to comment Share on other sites More sharing options...
dylpotato Posted July 10, 2010 Author Share Posted July 10, 2010 Thank you Mchl and Ken! =] I can't believe I missed that. Also thank you BizLab for your help too. -Dylan Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.