Jump to content

[SOLVED] Checking if a variable is an integer?


Warptweet

Recommended Posts

I have a form that asks my users for the width and height of their submission in pixels, but I want to prevent my users from entering a letter or a word to try and trick the system.

 

Is there a way of checking if my variable $variable is an integer or a string (or even a combination)?

I'm pretty sure this is very easy, but for some reason I can't put my finger on it.

Link to comment
Share on other sites

Doh! I always use regex anyways. But no matter what you do I suggest after doing your check you do a cast to the datatype that it should be. Apparently regex has some oddities where $ doesn't necessarily mean the end of the line.

Link to comment
Share on other sites

$flag = is_int($var);

 

or

 

$matches = preg_match("^\d+$", $var);

if( $matches != 0 ) $flag = intval($var);

 

Okay, apparently is_int won't work.

So for your second code, how do I make it execute code inside { and } if it IS an int?

Or, does that make the variable an integer, so I don't need to do an if statement?

Link to comment
Share on other sites

Oh wow, I really gave you bad code, whoops! As others pointed out apparently is_int wont work.

 

Try this.

function IsInteger($val)
{
  $matches = preg_match("^\d+$", $val);
  if( $matches != 0 )
  {
     return true;
  }
  return false;
}

 

usage:

$val = "5";
$is_int = IsInteger($val);
if( $is_int )
{
  $val = intval($val);
} 
else
{
  echo "ERROR! Not an int!";
}

Link to comment
Share on other sites

Hrmm. A thought occured to me so I'm going to throw it out here and see what some of the other people think. I think it may not be a "best practice" but here it is anyways.

 

Try this.

function IsInteger(&$val)
{
  $matches = preg_match("^\d+$", $val);
  if( $matches != 0 )
  {
     $val = intval($val);
     return true;
  }
  return false;
}

 

usage:

$val = "5";
$is_int = IsInteger($val);
if( $is_int )
{
  //Process normally... we don't need to cast since we passed by reference
} 
else
{
  echo "ERROR! Not an int!";
}

Link to comment
Share on other sites

None of your code works.

 

How about keep this even simpler?

How can I check if a number is divisable by 1? And if it isnt, make $error .= "You entered an incorrect width.";

 

After all, a letter cannot be divisable by 1, therefore it should be a logical method.

Link to comment
Share on other sites

What do you mean it doesn't work? The first code I spewed was the suck but those last 2 should work fine. And yeah the madmans code should work fine, but its not perty! Whatever you do, turn it into a function and reuse the code. Don't write that stuff by hand each time.

 

On another note.... any thoughts from anyone about passing that by reference and doing the cast?

Link to comment
Share on other sites

Create a file called testme.php and put this code in it.

 

<?php

function IsInteger(&$val)
{
  $matches = preg_match("^\d+$", $val);
  if( $matches != 0 )
  {
     $val = intval($val);
     return true;
  }
  return false;
}

?>

<HTML>
<HEAD>
<TITLE>Testing...</TITLE>
</HEAD>

<BODY>

<?php
$test1 = "5";
$test2 = "Doh";

echo $test1 . " is ";

$is_int = IsInteger($test1);
if( $is_int )
{
  echo " an integer!<br />";   
} 
else
{
  echo " is NOT an integer!<br />";
}

echo "<br /><br />";

echo $test2 . " is ";

$is_int = IsInteger($test2);
if( $is_int )
{
  echo " an integer!<br />";   
} 
else
{
  echo " is NOT an integer!<br />";
}
?>

</BODY>
</HTML>

 

Put this on your webserver and then point your browser to the file.

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.