I often have the need to view data in arrays and use the html tag <pre> to do this. However, it becomes tedious and frustrating having to type the same thing out all the time.
My question, what is your technique for avoiding this annoyance of programming with PHP?
There is no way to view it nicely formatted format except for using <pre> tag. Alternatively you can create this function and use that instead:
function pretty_print(array $array){
echo '<pre>';
print_r($array);
echo '</pre>';
}
Now instead of print_r, you can use the pretty_print. No need to type <pre> every now and then :)
Install XDebug. Besides making print_r and var_dump a lot prettier (and more useful), it also has other very handy features.
function pre_($array)
{
echo '<pre>' . print_r( $array, true ) . '</pre>';
}
You could try something like this. Note it is untested.
function html_var_dump($obj)
{
ob_start();
var_dump($obj);
$output = htmlentities(ob_get_contents());
ob_end_clean();
echo "<pre>$output</pre>";
}
You can use print_r instead of var_dump if you prefer.
I use this
function d($obj)
{
ob_start();
print_r($obj);
$output = htmlspecialchars(ob_get_clean());
echo "<pre>$output</pre>";
}
I change the default_mimetype (default text/html) php.ini setting to text/plain.
One of my favorite tricks, if printing the array is all I'm doing:
header('Content-type: text/plain');
print_r($arr);
Funny you should ask that, I just wrote a short function to save me the pain of having to do this so much.
function pre($option = "open"){
if (is_object($option) || is_array($option)):
print "<pre>";
print_r($option);
print "</pre>";
else:
$option=="open"?print "<pre>": print "</pre>";
endif;
}
If you pass an array or an object to it, it will print it inside pre tags. If you just want an opening tag, then do not pass an argument. If you want a closing tag, pass it any other argument (e.g. 1)
e.g.:
pre($result); //prints in pre tags
pre(); //just prints <pre>
print "hello";
pre(1); //just prints </pre>
Related
Often becomes handy to debug out the dump of a variable
Laravel's
dd($v);
dump($v);
are so useful.
I wonder if there's such a function that dumps the variable as a returned string instead of printing it out.
Like in php :
var_export($var, TRUE)
Note: I'm asking for a specific Laravel function not the built in PHP function , var_dump, var_export or print_r. I already know about it.
You can use the pre tag to format your output
echo "<pre>";
print_r($data);
echo "</pre>";
Edited
If you want to store the dump in a variable, I guess you can do like below
Import use Illuminate\Support\Debug\Dumper;
$data = User::all(); // change it according to your requirement
array_map(function ($data) {
(new Dumper)->dump($data);
}, func_get_args());
I couldn't find anything, so I resort to this:
$string = var_export($data, true);
Log::info($string);
In PHP, is it possible to write out an array within an echo command (or vice-versa)?
Thank you!
Sure, print_r() has an extra option to allow it to return the formatted text, instead of directly outputting it:
echo print_r($array, true);
not sure why you'd want to but you could possibly do
$var = print_r($array, true);
echo 'stack ' . $var;
to get the result your looking for
echo print_r($myVariable, true);
The second param signals the function to return the output rather than a boolean of success.
Given the following php5 code that output a gigantuous amount of difficult to read code:
<?=var_dump($_SERVER);?>
<?=print_r($GLOBALS); ?>
Question: how to make the output more human-readable? e.g. houw to but every "item" on a new line?
You can just wrap a pre-element around it:
<pre><?php var_dump($_SERVER); ?></pre>
<pre><?php print_r($GLOBALS); ?></pre>
Also note that <?= requires short_open_tags to be set to true (which is false in newer versions of php)
On your development environment, you should install the Xdebug extension.
Amongst other useful features (such as a debugger !), it'll get you nicer var_dump() :
colors
formating
For example, here's a screenshot of the beggining of the output I get for var_dump($_SERVER); :
(source: pascal-martin.fr)
You can use <pre> tag to format the output
<pre><?=print_r($GLOBALS); ?></pre>
Like everyone else mentioned, you can wrap that in <pre> tags to make it readable. I usually have the following 2 functions in my code at all times. Used as utility functions, inspired by cake.
function pr() {
$vars = func_get_args();
echo '<pre>';
foreach ($vars as $var) {
print_r($var);
}
echo '</pre>';
}
function prd() { //dies after print
$vars = func_get_args();
echo '<pre>';
foreach ($vars as $var) {
print_r($var);
}
echo '</pre>';
die();
}
Apart from the <pre> trick, you can try using dbug
Makes things much nicer and clearer: dBug
the previous answers suggest good solution, but if you want more control on the output you can run a loop over the arrays.
$_SERVER and $_GLOBALS are arrays, so you can do
foreach($_SERVER as $key=>$value){
echo $key . ' is ' . $value . '<br />' . PHP_EOL;
}
you can also add if statements to ignore some items in $_SERVER/$_GLOBALS
It's not whatever "server headers" but regular arrays.
To output array contents, a programmer usually makes use of a loop, and then format output in the manner they wish:
.
foreach($_SERVER as $key => $value){
echo "<b>$key:</b> $value<br>\n";
}
Note that your output being gigantic only because you're printing out the contents of $GLOBALS variable, which being completely useless for you.
please see: http://pastebin.com/5za3uCi1
I'm quite new to php and I'm editing the ventrilo status script. What I'd like it to do is that it stores everything in one big variable for easy parsing instead of using separate echo's. Can someone tell me how I can accomplish this?
Thanks,
Dennis
You can use the output buffer and get the contents of it:
ob_start();
echo 'foobar';
$contents = ob_get_contents(); // now contains 'foobar'
ob_end_clean();
declare a variable at the beginning, say $data or whatever. then, replace the echo calls:
echo "hello";
with this:
$data .= "hello";
then return the $data variable at the end of the function.
Instead of the echo, you can use a simple affectation :
$request = "CVentriloStatus->Request() failed. <strong>$stat->m_error</strong><br><br>\n";
But you'll soon have issues to manage multiple variables.
You could create an object to handle and store your information, but If you need something easy to set up and simple to operable, I'd go for arrays :
$ventriloStatus = array();
$ventriloStatus['requestObj'] = $stat->Request();
$ventriloStatus['requestMsg'] = "CVentriloStatus->Request() failed. <strong>$stat->m_error</strong><br><br>\n";
Add your data using keys.
Then retrieve the value easily :
echo $ventriloStatus['requestMsg'];
You can even parse your data using a simple loop
foreach($ventriloStatus as $key => $value){
echo $key.' : '.$value.'<br />';
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.
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
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.
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.
I basically use:
echo "<pre>"; print_r($_POST) ; echo "</pre>";
It prints the post values in a nice formatted way.
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..
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";
}
}
?>
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).
This shows more than just POST variables, but it's about as easy as it gets.
<?php
phpinfo(INFO_VARIABLES);
?>
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!
use print_r($_POST); or var_dump($_POST);
you can always display var echo command:
echo ($_POST['value']);
Very simply,
phpinfo();
It includes a listing of all variables passed to PHP from a form, in an easy-to-read format.