Jump to content

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.

is_int won't work because all data retrieved via POST or GET is actually a string. Use is_numeric().

then after using thorpes suggestion use int

sample

<?php
echo (int) ( (0.1+0.7) * 10 ); // echoes 7!
?> 

 

note numeric accept" + " and etc....see thorpes link

$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?

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!";
}

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!";
}

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.

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?

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.

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.