echo print_r and simple print_r

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hilal6
Forum Newbie
Posts: 6
Joined: Fri Jun 26, 2015 1:15 pm

echo print_r and simple print_r

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: echo print_r and simple print_r

Post 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.
hilal6
Forum Newbie
Posts: 6
Joined: Fri Jun 26, 2015 1:15 pm

Re: echo print_r and simple print_r

Post by hilal6 »

thanx
Post Reply