Jump to content

[SOLVED] is_int VS is_numeric noobish question


Recommended Posts

hey guys

 

when i check this :

 

if (is_int($_POST[number]))

 

it always return false , seems like $_POST always send a string .

is_int will return true for 2, NOT for "2" . 

 

how can i check an input that it is numeric ?  ( is_numeric allow float too but i  just whole numbers.)

and i don't mean by preg_match .

 

thanks in advance

 

is_numeric will take "2" and cast it as a number and then test to see if it is a number

 

Return Values

 

Returns TRUE if var is a number or a numeric string, FALSE otherwise.

 

maybe you need to do

 

if(is_numeric($_POST['number']) && is_int($_POST['number'])){}

 

 

is_numeric will take "2" and cast it as a number and then test to see if it is a number

 

Return Values

 

Returns TRUE if var is a number or a numeric string, FALSE otherwise.

 

maybe you need to do

 

if(is_numeric($_POST['number']) && is_int($_POST['number'])){}

 

 

 

That will always return false....

 

is_numeric does the casting internally and the variable isn't passed by reference.  That means, the second time, it will still be a string....

I don't know.... I hadn't ever heard of it either until the other day when someone posted about it in a thread similar to this thread.

 

Converting the types and comparing (forcing int) is actually a little bit faster, but it's obviously more code, and it's like .00000000000000001 seconds faster.

is_numeric will take "2" and cast it as a number and then test to see if it is a number

 

Return Values

 

Returns TRUE if var is a number or a numeric string, FALSE otherwise.

 

maybe you need to do

 

if(is_numeric($_POST['number']) && is_int($_POST['number'])){}

 

 

 

 

That will always return false....

 

is_numeric does the casting internally and the variable isn't passed by reference.  That means, the second time, it will still be a string....

 

i think you are confused. you want to change what is held in the $_POST var by casting it as an integer

$int = (int) $number;

not me. I want to check to see if what is in there is a number, and if it is an integer

 

I doubted my logic since you brought it into question and created a function to test what I was saying. Run it

 

<?php


function numberAndIntegerCheck($x){
    if(is_numeric($x) && is_int($x)){
        echo '<br />$x is a number and an integer!';
    }else{
        echo '<br />fail!';
    }
}

numberAndIntegerCheck(4);
numberAndIntegerCheck("4");
?>

C:\Users\Corbin\Desktop>php post.php

<br />$x is a number and an integer!<br />fail!

C:\Users\Corbin\Desktop>

 

"That will always return false...."

 

Unless you do like you said and cast the $_POST vars, that will indeed always return false, since $_POST variables are strings.

 

If you meant doing that, like you said in your second post, then I guess I missed that part.

$number = $_POST['number'];
$int = (int) $number;
if($int == $number) {
     //integer
}

 

additionally, if you want to correct code, this doesnt make any sense.

 

you are casting $number as an intger and using a == to check against dataypes - wrong! when checking datatypes you use the triple equal sign ===

 

and you dont even need to compare the two vars, just trying to cast a new datatype is check enough

 

$x = "asfdasf4";
if((integer) $x){
    echo 'that is a number';
}

Ahhh I didn't know you could do an if on a cast.

 

 

Also,

 

$number = $_POST['number'];
$int = (int) $number;
if($int === $number) {
     //this would always be false
}

 

$number would be a string, and $int would be an integer, thus, they need to be compared with == so that $int will be converted back to a string for comparison.

 

Edit:  (Actually, it might convert $number to a integer, in which case, this code would break since if $number failed at conversion the first time and was set to zero, it would fail the second time, and the two 0s would match.)

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.