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
How to Get the Query Executed in Laravel 5? Db::Getquerylog() Returning Empty Array
Difference Between If () { } and If (): Endif;
Best Practice on PHP Singleton Classes
How to Prevent My Site Page to Be Loaded Via 3Rd Party Site Frame of Iframe
Listing All the Folders Subfolders and Files in a Directory Using PHP
Need to Write At Beginning of File With PHP
PHP Json_Encode Class Private Members
Posting Raw Image Data as Multipart/Form-Data in Curl
PHP: Access Array Value on the Fly
Insert Multiple Rows With One Query MySQL
Simple PHP Strpos Function Not Working, Why
PHP Error: Cannot Modify Header Information - Headers Already Sent