Jump to content

print_r automatically echos contents


cs.punk

Recommended Posts

Hey guys it's been a long time, but I am back with php!

 

Anyway here's my dilemma:

 

test.php

<?php
<?php
$name = array('Chris', 'Bob', 'Jack');

$names = print_r($name);
?>

 

Simple enough right? Well although I am not printing/echoing these results to screen it does it automatically.

 

Meaning when I point my browser to test.php it outputs this:

Array ( [0] => Chris [1] => Bob [2] => Jack ) 

 

Rather than only assigning the value to the $names variable, which is what I would expect it to do. Is this normal behaviour or is there something I can change on php.ini?

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/226883-print_r-automatically-echos-contents/
Share on other sites

Hi cs.punk,

You can capture the output of print_r instead of echoing it by adding a return value:

 

print_r($myArray, true);

 

Try this with your code:

<?php

$name = array('Chris', 'Bob', 'Jack');

$names = print_r($name, true);
echo $names;
?>

 

cheers,

Fergal

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.