2

Can anyone please explain, why do I get very strange warning:

filter_input() expects parameter 1 to be long, string given

when executing the code, that is part of my class and which seems perfectly fine:

public static function Input($type, $data, $filter = 'FILTER_SANITIZE_SPECIAL_CHARS')
  {
    $type = 'INPUT_' . $type;
    return filter_input($type, $data, $filter);
  }

In case I change it to, for example:

return filter_input(INPUT_POST, $data, $filter);

Then the warning goes to:

filter_input() expects parameter 3 to be long.

Everything works just fine if I use:

return filter_input(INPUT_POST, $data, FILTER_SANITIZE_SPECIAL_CHARS);


I realize that on PHP: filter_input - Manual in description it's stated:

Description

mixed filter_input ( int $type , string $variable_name [, int $filter = FILTER_DEFAULT [, mixed $options ]] )

Parameters

type
   One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.


Questions:

  1. Why it's said in manual filter_input ( int $type , - when neither INPUT_GET nor INPUT_POST and etc are INTEGERS.
  2. Is there a way to pass a value into filter_input using variable?

3 Answers 3

7

What you're supposed to use there are constants. These constants have integer values. So the documentation is entirely correct, INPUT_GET is an integer. Try var_dump(INPUT_GET).

If you need to get a constant value from a string, use constant():

echo constant('INPUT_' . $type);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, so easy and straight-forward!! Never heard of that before!! It works as expected! Thank you!
1

Here the problem. When you concatenate 'INPUT_' with variable it bacame a string, see example:

echo $type = 'INPUT_' . 'POST'; // give you a string INPUT_POST

echo INPUT_POST; //give you 0

That's why :

filter_input() expects parameter 1 to be long, string given

3 Comments

sergio, thanks but I'm not sure what you have meant!
When you do this: $type = 'INPUT_' . $type; it became a string, but not a constant like INPUT_POST.
Ah, alright! Yeah, deceze already explained that! +1 for an extra example!
0

INPUT_POST and INPUT_GET are defined as follows:

/**
 * POST variables.
 * @link http://www.php.net/manual/en/filter.constants.php
 */
define ('INPUT_POST', 0);

/**
 * GET variables.
 * @link http://www.php.net/manual/en/filter.constants.php
 */
define ('INPUT_GET', 1);

2 Comments

Thank you, Rico! Could you please elaborate your answer, please!
See @deceze answer for a final solution. I only wanted to point out the type of the constants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.