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?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
abkrim
  • 3,512
  • 7
  • 43
  • 69

2 Answers2

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) }
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Well i use this solution. But I question it's diferent. My poor english not explain question clearly. Aprecite your response. – abkrim May 21 '15 at 16:32
  • @abkrim What else is then your question? If it is a runtime option? The answer would be no. – Rizier123 May 21 '15 at 16:33
  • @abkrim And your question is why the output is formatted differently than yours? – Rizier123 May 21 '15 at 16:35
  • Ohh my goog... i see code of web page, and put
     tags... I apologize for inconvenients. Not expert on php. Best regards
    – abkrim May 21 '15 at 16:37
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 ;)

Michael Walter
  • 1,427
  • 12
  • 28