Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[PHP] Finer points of debugging: vardump vs. print_r

3 views
Skip to first unread message

Plutarck

unread,
Apr 9, 2001, 8:38:46 PM4/9/01
to
I use print_r on arrays, and var_dump on everything else.

print_r will not tell you if a variable is NULL, but var_dump will. So I
usually use both.


--
Plutarck
Should be working on something...
...but forgot what it was.


""John Lim"" <heyjo...@yahoo.com> wrote in message
news:9asuq0$f1n$1...@toye.p.sourceforge.net...
> Just a question that has been besetting me for a while:
>
> which is better for debugging -- vardump( ) or print( ) ?
>
> Does anyone have a preference? Why? Is one better than the other?
> Thanks for answering this prickly question!
>
> John Lim
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general...@lists.php.net
> For additional commands, e-mail: php-gene...@lists.php.net
> To contact the list administrators, e-mail: php-lis...@lists.php.net
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general...@lists.php.net
For additional commands, e-mail: php-gene...@lists.php.net
To contact the list administrators, e-mail: php-lis...@lists.php.net

Morgan Curley

unread,
Apr 9, 2001, 5:12:58 PM4/9/01
to
I generally include the following function:
function dump( $label, $input ){
echo( "<PRE>" );
echo( "<H2>$label</H2>\n" );
print_r( $input );
echo( "</PRE>" );
return 1;
}

or if you like javascript this pops up an alert wherever you insert the
dump call in your page
function dump( $label, $input ){
ob_start();
print_r( $input );
$gc = ob_get_contents();
ob_end_clean();

$gc = preg_replace( "/'/", "\\'", $gc );
$gc = preg_replace( '/\n/', '\\n', $gc );
echo( "<script>alert( '$label\\n".$gc."' );</script>" );
return 1;
}

then for simple debugging I call
dump( "My Label", $my_var );

With the javascript you can do some cool things like having divs generated
per $label.

morgan

Philip Olson

unread,
Apr 9, 2001, 4:06:35 PM4/9/01
to

Here's some info about the two functions.

Descriptions (from manual) :
--------------------------------
print_r -- Prints human-readable information about a variable
(PHP 4)

var_dump -- Dumps information about a variable
(PHP 3 >= 3.0.5, PHP 4)

An example :
--------------------------------
<pre>
<?php
$example = array ('str' => 'is good',
'letters' => array ('a','b','c'),
'integer' => 31,
'float/double' => 35.3787,
24 => 'hello');

echo 'print_r :'. "\n\n";
print_r ($example);

echo 'var_dump :'. "\n\n";
var_dump ($example);
?>
</pre>

Output (layout altered) :
---------------------------------

print_r :

Array
(
[str] => is good
[letters] => Array
(
[0] => a
[1] => b
[2] => c
)
[integer] => 31
[float/double] => 35.3787
[24] => hello

)

var_dump :

array(4)
{
["str"] => string(7) "is good"
["letters"] => array(3)
{
[0] => string(1) "a"
[1] => string(1) "b"
[2] => string(1) "c"
}
["integer"] => int(31)
["float/double"] => float(35.3787)
[24] => string(5) "hello"

}

Manual Entries :
--------------------------------
http://www.php.net/manual/en/function.print-r.php
http://www.php.net/manual/en/function.var-dump.php


Regards,
Philip


(test: print var dump faq8490)

On Tue, 10 Apr 2001, John Lim wrote:

> Just a question that has been besetting me for a while:
>
> which is better for debugging -- vardump( ) or print( ) ?
>
> Does anyone have a preference? Why? Is one better than the other?
> Thanks for answering this prickly question!
>
> John Lim
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general...@lists.php.net
> For additional commands, e-mail: php-gene...@lists.php.net
> To contact the list administrators, e-mail: php-lis...@lists.php.net
>


Philip Olson
http://www.cornado.com/

John Lim

unread,
Apr 9, 2001, 2:25:10 PM4/9/01
to

John

unread,
Apr 10, 2001, 2:52:50 AM4/10/01
to
Hi Morgan,

You tip is fantastic. Will post it (with your permission) at my weblog,
http://php.weblogs.com/ tomorrow.

Regards, John

"Morgan Curley" <mcu...@e4media.com> wrote in message
news:4.3.2.7.2.200104...@e4media.com...

0 new messages