Avatar of jeff_zucker

asked on 

Simple PHP function

I have a bunch of php scripts that need to sanitize integers.  It seems inefficient to do this:
$var1 = filter_input(INPUT_GET, 'var1',FILTER_SANITIZE_NUMBER_INT);
$var2 = filter_input(INPUT_GET, 'var2',FILTER_SANITIZE_NUMBER_INT);
$var3 = filter_input(INPUT_GET, 'var3',FILTER_SANITIZE_NUMBER_INT);

I don't want to use classes (I have my reasons).  Looking for some quick code to do this efficiently.

PHPWeb Development

Avatar of undefined
Last Comment
Avatar of oleggold
Flag of United States of America image

why don't just have 1 sanitizing var,e.g:
$sanit_var = filter_input(INPUT_GET, 'var1 ',FILTER_SANITIZE_NUMBER_INT);
$var1 = $sanit_var;
$sanit_var = filter_input(INPUT_GET, 'var2 ',FILTER_SANITIZE_NUMBER_INT);
$var2 = $sanit_var;
or even better ,write a tiny return of sanitized value so sanitation only happens on demand ,less code and less memory.

Avatar of Beverley Portlock
"Question: I have a bunch of php scripts that need to sanitize integers.  It seems inefficient to do this:...."

How do you know it is inefficient? Have you checked the algorithm?

PHP code running in memory is very fast. You will make no noticeable savings from doing this in other ways even if you are checking thousands of integers. Each of these operations takes about 2 microseconds so even if you cut it down by 90% for 100 operations you will save 180 microseconds. Do you think anyone will notice that?

Avatar of jeff_zucker


I just reread my original post and i don't think I explained it that well.  I have a ton of variables, all with different names that need to be sanitized.  It seems inefficient to have to copy/paste
$var1 = filter_input(INPUT_GET, 'var1',FILTER_SANITIZE_NUMBER_INT);
Then edit $var1 to be $var2, then edit 'var1' to be 'var2', etc. and to do this hundreds of times for each variable name.

Ideally, I just create an array and feed it to a function.  I'm looking for help with writing that function.
That is indeed a different problem. All they all called something like $var1 $var2 ..... $var981 and so on?

Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
why so many times... use a function ...

try this...

function sanitizeInt($data){
$r = filter_var($data,FILTER_SANITIZE_NUMBER_INT);
return $r;

Open in new window

Reference for your help : http://www.php.net/manual/en/function.filter-var.php
Avatar of jeff_zucker


This is what i was looking for.
have you tried my solution.... no I am not arguing over points... silly people do that ... I am just saying try that piece of code.... its quite clean and efficient ...
Avatar of jeff_zucker


kshna: Thanks for your help.  I'm sure yours would work too.  I had already started to implement the first solution, so I kept going with it.  Sorry about that.  I'll take a look later to see what is the correct thing to do in this situation where there are multiple good answers.  

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo