Mar 29, 2008 - PHP    13 Comments

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 Comments

  • 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)

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

  • 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!

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

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

  • 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 ;-)

  • @ Rahul

    Actually $_POST is an array itself.

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

  • beautiful website mate!!
    cheers!

  • foreach($_POST as $key=>$val) {
    echo “Post named $key has the value $val “;
    }

    foreach($_GET as $key=>$val) {
    echo “Post named $key has the value $val “;
    }

  • Thanks @Adrian-Ionut, that was a more appropiate way to display all form values for me.

  • 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!

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

  • You’re a life and a many day saver, thankyou for this!

Got anything to say? Go ahead and leave a comment!