Avatar of jeff_zucker
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
jeff_zucker

8/22/2022 - Mon
oleggold

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.

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?

jeff_zucker

ASKER
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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Beverley Portlock

That is indeed a different problem. All they all called something like $var1 $var2 ..... $var981 and so on?

ASKER CERTIFIED SOLUTION
Beverley Portlock

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Sandeep Kothari

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
jeff_zucker

ASKER
This is what i was looking for.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Sandeep Kothari

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 ...
jeff_zucker

ASKER
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.