Learning Is Fun

Talks on Web Technology and Better Product Development

PHP script to print all the GET & POST variables

March29

Variables are one of the core powers of programming. As PHP programmers, we oftern may find that a $_GET or $_POST variable is not performing properly or the way we expected. This may occur for several reasons such as we made a mistake in the variable name. Sometimes this kind of problem take so much time that lots of time is wasted to correct a single variable!

So, you can use the scripts I have written below and use in your code whenever you suspect that there may be a problem with the variables. These scripts helps you by showing the details of each and every $_GET and $_POST variables in the script.

This is very easy and actually 3 (three) lines of code required.

Below is the code to print all $_GET variables:

<?php

print(‘<pre>’);
print_r($_GET);
print(‘</pre>’);

?>

Click here to see the demo here.

To print all the $_POST variables, we need to change only one line:

<?php

print(‘<pre>’);
print_r($_POST);
print(‘</pre>’);

?>

OK.
You can do it in another way too. It is simple also.

<?php

var_dump($_GET);

?>

Or,

<?php

var_dump($_POST);

?>

So, print your $_GET and $_POST variables whenever you think required.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
posted under PHP
7 Comments to

“PHP script to print all the GET & POST variables”

  1. On March 30th, 2008 at 9:02 pm RA Says:

    I came across you site while I was looking for some smart Bangladeshi coder who has passion for CSS.

    If you pass second param true then print_r outputs to a variable. That you can show the result in any place!

    $out = print_r($_POST, true)

  2. On March 30th, 2008 at 9:46 pm admin Says:

    Hello RA,

    Yes. You are right.
    Storing the value in a variable will give the coder flexibility to test the availability of the variables at any position in the page.

    And thanks for the comment.

  3. On November 13th, 2008 at 11:09 am FajitaMaster Says:

    You could also use the ever so clever php $_REQUEST

    echo ”;
    var_dump($_REQUEST);
    echo ”;

    It gets both GET or POST values … now thats what I call universal!

  4. On November 13th, 2008 at 11:14 am admin Says:

    @ FajitaMaster

    Yes.

    You are right.
    But in that case you do not know which are your POST or GET variables.
    If you check the PHP manual, you will see that $_REQUEST use is discouraged.

  5. On October 31st, 2009 at 9:46 pm Krinkle Says:

    If you want the variables in a bit more clear on your screen, here’s a script I use on pretty much every ocasion I need to var_dump something:

    http://pastebin.com/f3d201b3c

    Basicly the same but makes a sticky terminal like box on the top left of the page. The box will be no more then 90% of the height if the document (just in case there are a looot of varibales, the overflow-y: is set to auto, so it’ll show a scrollbar when neccerary).

    For the rest it’s devided in both POST and GET, with each one on a new line.

    Handy :)
    Feel free to customize to your liking.

  6. On November 26th, 2009 at 12:07 pm Rahul Says:

    Hi,

    Can you help me with this:

    I need to store all the post variables displayed in “print_r($_POST);” in an array so that I can compare it with a MySQL table.

    Is there a way to do it ?

    Thanks in advance ;-)

  7. On November 26th, 2009 at 1:41 pm admin Says:

    @ Rahul

    Actually $_POST is an array itself.

    So, try to get its keys and values as like other variables.
    :)

Email will not be published

Website example

Your Comment: