Hướng dẫn php print_r new line

When I run the following code:

echo $_POST['zipcode'];

print_r[$lookup->query[$_POST['zipcode']]];

?>

the results are concatenated on one line like so: 10952Array.

How can I get it to display on separate lines, like so:

08701
Array

asked Dec 15, 2011 at 7:10

You might need to add a linebreak:

echo $_POST['zipcode'] . '
';

If you wish to add breaks between print_r[] statements:

print_r[$latitude]; 
echo '
'; print_r[$longitude];

answered Dec 15, 2011 at 7:12

spanspan

5,5688 gold badges54 silver badges111 bronze badges

2

to break line with print_r:

echo "
";
    print_r[$lookup->query[$_POST['zipcode']]];
echo "
";

The element will format it with any pre-existing formatting, so \n will turn into a new line, returned lines [when you press return/enter] will also turn into new lines.

//developer.mozilla.org/en-US/docs/Web/HTML/Element/pre

brandito

6878 silver badges19 bronze badges

answered Jun 12, 2017 at 16:08

Sen SokhaSen Sokha

1,68614 silver badges15 bronze badges

1

If this is what your browser displays:

Array [ [locus] => MK611812 [version] => MK611812.1 [id] => 1588040742 ]

And this is what you want:

Array
[
    [locus] => MK611812
    [version] => MK611812.1
    [id] => 1588040742
]

the easy solution is to add the the

 format to your code that prints the array:

echo "
";
print_r[$final];
echo "
";

answered Jun 14, 2019 at 8:59

charelfcharelf

2,6293 gold badges25 silver badges39 bronze badges

Old question, but I generally include the following function with all of my PHP:

The problem occurs because line breaks are not normally shown in HTML output. The trick is to wrap the output inside a pre element:

function printr[$data] {
    echo sprintf['
%s
',print_r[$data,true]]; }

print_r[…, true] returns the output without [yet] displaying it. From here it is inserted into the string using the printf function.

klewis

6,95914 gold badges56 silver badges97 bronze badges

answered Dec 10, 2018 at 7:16

ManngoManngo

11.9k8 gold badges70 silver badges91 bronze badges

2

Just echo these : echo $_POST['zipcode']."
";

answered Dec 15, 2011 at 7:12

Jigar TankJigar Tank

1,4541 gold badge13 silver badges23 bronze badges

[PHP 4, PHP 5, PHP 7, PHP 8]

print_r Prints human-readable information about a variable

Description

print_r[mixed $value, bool $return = false]: string|bool

print_r[], var_dump[] and var_export[] will also show protected and private properties of objects. Static class members will not be shown.

Parameters

value

The expression to be printed.

return

If you would like to capture the output of print_r[], use the return parameter. When this parameter is set to true, print_r[] will return the information rather than print it.

Return Values

If given a string, int or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects.

When the return parameter is true, this function will return a string. Otherwise, the return value is true.

Examples

Example #1 print_r[] example



The above example will output:

Array
[
    [a] => apple
    [b] => banana
    [c] => Array
        [
            [0] => x
            [1] => y
            [2] => z
        ]
]

Example #2 return parameter example

Notes

Note:

When the return parameter is used, this function uses internal output buffering prior to PHP 7.1.0, so it cannot be used inside an ob_start[] callback function.

See Also

  • ob_start[] - Turn on output buffering
  • var_dump[] - Dumps information about a variable
  • var_export[] - Outputs or returns a parsable string representation of a variable

liamtoh6 at hotmail dot com

12 years ago

I add this function to the global scope on just about every project I do, it makes reading the output of print_r[] in a browser infinitely easier.



It also makes sense in some cases to add an if statement to only display the output in certain scenarios, such as:

if[debug==true]
if[$_SERVER['REMOTE_ADDR'] == '127.0.0.1']

simivar at gmail dot com

5 years ago

I've fixed function wrote by Matt to reverse print_r - it had problems with null values. Created a GIST for that too so please add any future fixes in there instead of this comment section:
//gist.github.com/simivar/037b13a9bbd53ae5a092d8f6d9828bc3

$val] {
    if [
is_numeric[$key]] $key = "arr_".$key; //

lech

5 years ago

A slight amendment to Matt's awesome print_r_reverse function [Thank You, a life-saver - data recovery :-] . If the output is copied from a Windows system, the end of lines may include the return character "\r" and so the scalar [string] elements will include a trailing "\r" character that isn't suppose to be there. To resolve, replace the first line in the function with...



This will work for both cases [Linux and Windows].

I include the entire function below for completeness, but all credit to Matt, the original author, Thank You.

Chủ Đề