PHP script to print all the GET & POST variables

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.

13 thoughts on “PHP script to print all the GET & POST variables

  1. 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. 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. @ 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.

  4. 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.

  5. 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 ;-)

  6. Some interesting ways to do it here. Personally I found the loop by @Adrian-Ionut to be the most useful.

    Thanks to everyone for sharing the different approaches!

  7. I would need some help man. I want to make a small php script that will use the $_GET thing to redirect to a website that you specify with that $_GET. Is there any way to make this small script for me? Thank you :D

    PS: It’s for our site but me and my partner we are not really good coders on php hehe XD
    Send me an email or post here and I will check when you have a small sript like that :D

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>