Jump to content

PHP is_numeric() function not working. Help!


dylpotato

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.