phpfilter-input

Is there a way to simplify this function by using say filter_input


function valueFromGetOrPost($parameter)
{
    $shvalue=NULL;
    if ($_GET[$parameter])
    {
        $shvalue=$_GET[$parameter];
    }
    else if (isset($_POST[$parameter]))

    {
        $shvalue=$_POST[$parameter];
    }
    return $shvalue;

}

say by using filter_input

Basically the code check whether a parameter exist either in GET or POST. And then return the value of the parameter.

I think this must be so common it should be there by some built in function already


Solution

  • Use $_REQUEST (documentation).

    An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.

    So your code will look like:

    function valueFromGetOrPost($parameter)
    {
        $shvalue=NULL;
        if ($_REQUEST[$parameter])
        {
            $shvalue=$_REQUEST[$parameter];
        }
        return $shvalue;
    
    }