2

I see manual of php.net and see sortie of examples, when use var_dump and others commands for see examples.

All examples sort with pre style.

But on my own server I see same examples on only one line

var_dump($a);

On manual see->

array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}

On my server I see:

array(3) {[0]=>int(1) [1]=>int(2) [2]=>array(3) {[0]=>string(1) "a" [1]=>string(1) "b" [2]=>string(1) "c"}}

I think this is a runtime option, which I can change, but I'm not sure. So how can I get the output in the same format as in the manual?

5
  • Remove <pre> and try the same var_dump Commented May 21, 2015 at 15:55
  • That's not PHP, that's the browser you're dumping the output to. If you want the browser to display it nicely, you'll have to wrap appropriate html around the vardump output... Commented May 21, 2015 at 15:58
  • So where are we with this question ? Commented May 21, 2015 at 16:19
  • If you have xdebug installed/enabled then the output will be formatted automatically Commented May 21, 2015 at 17:15
  • Not xdebug on develop computer Commented May 22, 2015 at 7:42

2 Answers 2

5

You are probably looking for the pre tag, which will give you a nice formatted output. Just print it before you use var_dump();, e.g.

echo "<pre>";
var_dump($arr);
echo "</pre>";

Example input/output:

$arr = [1, 2, 3];

with pre tag:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

without pre tag:

array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
Sign up to request clarification or add additional context in comments.

4 Comments

Well i use this solution. But I question it's diferent. My poor english not explain question clearly. Aprecite your response.
@abkrim What else is then your question? If it is a runtime option? The answer would be no.
@abkrim And your question is why the output is formatted differently than yours?
Ohh my goog... i see code of web page, and put <pre></pre> tags... I apologize for inconvenients. Not expert on php. Best regards
0

I crated this function for this case.

function echo_array($array,$name = '')
{
    $debug = debug_backtrace();
    $file = $debug[0]['file'];
    $line = $debug[0]['line'];
    $array = "<pre>".print_r($array,true)."</pre>";
    ?>
    <table cellpadding="0" cellspacing="0" width="100%" style="background-color:white;">
      <tr>
        <td align="left">
          <fieldset>
            <legend>
              <?=$name?> in: <?=$file?>:<?=$line?>
            </legend>
            <?=$array?>
          </fieldset>
        </td>
      </tr>
    </table>
    <?
}

With print_r() can you pass the array and the second parameter says that it should be return instead of printed out.

I use the debug information to get where this function was called because sometimes I have to many of them, that it can get confusing. You can also set a name for it to determine what was echoed ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.