Using $_GET and $_POST together

I find myself writing PHP scripts to do backend Ajax work.  One of the problems I often come up against is that I need to be able to call the same script either using a query string and pulling the variables from $_GET or via a form and calling the variables from $_POST.  I usually use a variable named action to determine what the script needs to do and it would be easier if I could use one variable to reference both types.

Yes, I know about $_REQUEST.  The problem with that is that it includes $_COOKIES as well and I don’t want to get any more than I need.

So what I’ve started doing is using array_merge() to create a new variable that contains both $_GET and $_POST. 

PHP:
  1. $arrMerge = array_merge($_GET, $_POST);

Notice that the $_GET array comes before $_POST.  The way array_merge works in case of an overlapping string key the later array passed will overwrite anything previously added.  That way if I have both $_GET['action'] and $_POST['action'] $arrMerge will contain the $_POST['action'].

Question, Comments...

Do you have more questions. Please either leave a comment below or join us in our new forum.

Leave a Reply