What Does "1" Mean At the End of a PHP Print_R Statement

What does 1 mean at the end of a php print_r statement?

You probably have echo print_r($view). Remove the echo construct. And... what need do you have to parse its output? There are certainly much better ways to solve your problem.

PHP print_r adds a '1' to the end

Because print_r echos the output anyway, so you're basically saying echo echo $vArray

You can do the following so that it's returned instead of echo'd:

echo '<pre>' . print_r($vArray, true) . '</pre>';

What does the number outside an array printed with print_r mean?

The 1 you see is the truthy value of the print_r() function.

Because print_r() is a function in itself, it evaluates to truthy.

In your example, print_r() runs and outputs your array, followed by echo, which echoes out the truthy value of print_r().

To eliminate this erroneous 1, simply remove the echo from your code (which isn't needed, as print_r() outputs to the DOM by itself):

<?php
$array = [1,2,3,[4,34]];
print_r($array);
?>

Hope this helps :)

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

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.



Related Topics



Leave a reply



Submit