Why do print_r and var_dump execute before echo

后端 未结 3 393
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 01:52

I\'m calling either var_dump() or print_r() on an array that has one value in an echo statement:

echo \"
<
相关标签:
3条回答
  • 2020-12-12 02:25

    You can use the comma operator instead of the dot operator:

    echo "<pre>" . var_dump($cal) . "</pre>";
    
    object(Calendar)#2 
    <pre>
    </pre>
    
    echo "<pre>" , var_dump($cal) , "</pre>";
    
    <pre>object(Calendar)#2 
    </pre>
    
    0 讨论(0)
  • 2020-12-12 02:33

    They do it because they aren't returning data, they are echoing. You can do print_r($array, true) to make it return, but var_dump() will need output buffering.

    If you want it to work the way you're trying to make it, separate them into distinct calls.

    0 讨论(0)
  • 2020-12-12 02:45

    var_dump doesn't return anything, it does its own printing. It evaluates first, since PHP can't concatenate an expression of which it doesn't know the value. You probably want:

    echo "<br><br>testArray is ==> ";
    var_dump($testArray);
    echo " <===<br><br>";
    

    instead. Print the start, the middle, then the end.

    0 讨论(0)
提交回复
热议问题