Page 1 of 1

echo print_r and simple print_r

Posted: Thu Jul 30, 2015 2:55 am
by hilal6
In the following code

***** PLEASE USE PHP CODE TAG *****

Code: Select all

<?php
$a=array(2,3,4,5,6,array(1,3,4,4,34,5,array(1,3,23,"cat",5,34)));

$a[5][6][3]="dog";
?>
<br><pre>
<?php

print_r($a);

?></pre>
i dont get a 1 at the end of the result but if i use the code echo print_r($a) i do get a 1 at the end. Which is the right way of coding and why is the one there?

Re: echo print_r and simple print_r

Posted: Thu Jul 30, 2015 3:23 am
by requinix
print_r() is outputting stuff. It will return true. If you echo it then you will see the 1 because that's what true looks like when you output it.

No echo.

Alternatively, you can make print_r() return stuff instead of outputting stuff by using its second argument (check the documentation). In that case you would have to use echo because otherwise nothing will get outputted. But there's no point to doing it like that because you're doing extra work that you don't need to do.

Re: echo print_r and simple print_r

Posted: Thu Jul 30, 2015 4:45 am
by hilal6
thanx