Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing POST array to php function

Tags:

php

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?

like image 477
mrpatg Avatar asked Nov 02 '09 12:11

mrpatg


2 Answers

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

like image 63
Matthew Scharley Avatar answered Oct 09 '22 21:10

Matthew Scharley


You can actually pass $_POST to any function which takes in an array.

function process(array $request)
{

}

process($_POST);
process($_GET);

Great for testing.

like image 25
Extrakun Avatar answered Oct 09 '22 22:10

Extrakun