PHP Snippet - Request-Argument From Either POST Or GET

Posted at

In-case you would like to enable a request details from either POST or GET (and you do not really care from where it got from)- this is the proper way to do this.

*consider filtering the input, which should be handled depending on the content you expect to receive.

function arg_value($name){ /*returns a valid value or empty string. valid argument from either POST or GET */
  $items = [
            filter_input(INPUT_POST, $name, FILTER_UNSAFE_RAW)
           ,filter_input(INPUT_GET, $name, FILTER_UNSAFE_RAW)

  $items = array_filter($items, function($item){
                                  $item = (null !== $item) && (false !== $item);
                                  return $item;
  array_push($items, null); /* null value */
  $items = array_shift($items);
  return $items;