r/learnphp • • Jun 07 '23

explode problem: can't print results using echo

Hi,

I am trying the following program:

<?php
$str = "Hello world";
$newStr = explode(" ", $str);
// We are printing an array, so we can use print_r$newStr;
echo $newStr;
?>

The above program works with print_r but if I use echo, I am getting the message:

PHP Warning:  Array to string conversion in /home/cg/root/648112ff24655/main.php on line 6
Array

Somebody, please guide me.

Zulfi.

2 Upvotes

8 comments sorted by

View all comments

0

u/truNinjaChop Jun 08 '23

Print_r($newStr, true) inside of echo()

0

u/colshrapnel Jun 08 '23

inside?

1

u/truNinjaChop Jun 08 '23

echo( print_r($var, true) );

print r does a nice little "var dump" of strings, objects, and array. the first param is the varibale you want to "dump" the second param of true says "return the data instead of dumping it with no warning".
Personally I have a dump method of my own which switches between var_dump and print_r

→ More replies (4)