20

I have a form that has multiple fields, and for testing purposes is there a way I could print out the values entered in all the fields, without having to individually print each value.

Xenph Yan
  • 83,019
  • 16
  • 48
  • 55
Brad
  • 12,054
  • 44
  • 118
  • 187

11 Answers11

26

You should be able to do a var_dump($_REQUEST);

http://us2.php.net/manual/en/reserved.variables.request.php

http://us2.php.net/manual/en/function.var-dump.php

defeated
  • 1,064
  • 11
  • 13
  • While this really answers the question very well, and works perfectly, I am wondering if having a var_dump of **user provided** data can be a security risk (if the php-doc that contains the var_dump) would be reachable from a untrusted source? Do you know if your suggested solution is also safe? – humanityANDpeace Oct 06 '13 at 08:00
  • 2
    @humanityANDpeace you shouldn't be using var_dump in a production environment, its really just for testing purposes. So the only user who could provide unsafe data should be you :) – Ted Avery Nov 13 '13 at 12:04
14

For extra credit, I always have:

function pre($data) {
    print '<pre>' . print_r($data, true) . '</pre>';
}

Whenever I need to debug an array - which is very often - I just do pre($arr); to get a nicely formatted dump.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
13

print_r() / var_dump() are simple and gets the job done.

If you want a styled/dynamic option check out Krumo:

http://krumo.sourceforge.net/

A lot of developers use print_r() and var_dump() ... Krumo is an alternative: it does the same job, but it presents the information beautified using CSS and DHTML.

Community
  • 1
  • 1
micahwittman
  • 12,356
  • 2
  • 32
  • 37
8

I basically use:

echo "<pre>"; print_r($_POST) ;  echo "</pre>";

It prints the post values in a nice formatted way.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rinzler
  • 2,139
  • 1
  • 27
  • 44
7

If you pay close attention to the $_POST[] or $_GET[] method, you will realize that both of them are actually arrays.This means that you can play around with them just like you do with any other arrays.

For example, you can print_r($_POST) and you will see everything the way were entered..

Whiler
  • 7,998
  • 4
  • 32
  • 56
ajmakoni
  • 79
  • 1
  • 1
4

This PHP code doesn't require any knowledge of the fields in the form that submits to it, it just loops through all of the fields, including multiple-choice fields (like checkboxes), and spits out their values.

<?php
// loop through every form field
while( list( $field, $value ) = each( $_POST )) {
   // display values
   if( is_array( $value )) {
      // if checkbox (or other multiple value fields)
      while( list( $arrayField, $arrayValue ) = each( $value ) {
         echo "<p>" . $arrayValue . "</p>\n";
      }
   } else {
      echo "<p>" . $value . "</p>\n";
   }
}
?>
Tim
  • 813
  • 5
  • 14
2

If you're debugging a lot, I would recommend installing XDebug. It makes var_dump's very pretty and useful (giving you the type and length of the variable aswell).

Rexxars
  • 1,167
  • 8
  • 10
1

This shows more than just POST variables, but it's about as easy as it gets.

<?php
    phpinfo(INFO_VARIABLES);
?>
tmecham
  • 349
  • 3
  • 4
0

Besides using inline debug statements, you could also considering transient debugging, i.e. you could use an IDE with debug capabilities, like eclipse or zend studio. This way you could watch any variable you'd like to.

bye!

Hermooz
  • 39
  • 1
0

use print_r($_POST); or var_dump($_POST);
you can always display var echo command:

echo ($_POST['value']); 
Damiano93
  • 19
  • 6
-5

Very simply,

phpinfo();

It includes a listing of all variables passed to PHP from a form, in an easy-to-read format.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131