Can i pass the entire POST array into a function and handle it within the function?
such as
PostInfo($_POST);
function PostInfo($_POST){
$item1 = $_POST[0];
$item2 = $_POST[1];
$item3 = $_POST[2];
//do something
return $result;
}
or is this the correct way of doing this?
Yes. If you are going to name the local variable $_POST
though, don't bother. $_POST
is a 'superglobal', a global that doesn't require the global
keyword to use it outside normal scope. Your above function would work without the parameter on it.
NOTE You cannot use any superglobal (i.e.
$_POST
) as a function argument in PHP 5.4 or later. It will generate a Fatal error
You can actually pass $_POST to any function which takes in an array.
function process(array $request)
{
}
process($_POST);
process($_GET);
Great for testing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With