Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a 1 at the end of my printed array?

This is a super simple array print, but I'm getting at the end when I use print_r.

<?php 
  $user_names = array(1, 2, 3, 4);
  $results = print_r($user_names);
  echo $results;
?>

Then I get:

 Array
 (
     [0] => 1
     [1] => 2
     [2] => 3
     [3] => 4
 )
 1
like image 859
Peter Avatar asked Dec 21 '22 11:12

Peter


1 Answers

print_r already prints the array - there is no need to echo its return value (which is true and thus will end up as 1 when converted to a string):

When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.

The following would work fine, too:

$results = print_r($user_names, true);
echo $results;

It makes no sense at all though unless you don't always display the results right after getting them.

like image 67
ThiefMaster Avatar answered Dec 23 '22 01:12

ThiefMaster