Page 1 of 1

Implode, then explode, and weirdly a digit shows up

Posted: Mon Jul 04, 2011 7:41 pm
by balikhan
Hi all,
I just can't find anything on the internet with regards to this issue (not much of an issue to be honest, just curious why this is happening). So here's the code:

Code: Select all

<?php $array1 = array(1,2,3,5,8,13,21); ?>

<?php echo $string1 = implode(" * ",$array1); ?><br/>

<pre><?php echo print_r(explode(" * ",$string1)); ?></pre><br />
and this gives me the following output:

1 * 2 * 3 * 5 * 8 * 13 * 21
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 5
[4] => 8
[5] => 13
[6] => 21
)
1

Notice that weird "1" at the end? For the life of me I can't figure out why it's there. I'm using PHP 5.3 (latest installation of xampp).

Thanks in advance.

Bali.

Re: Implode, then explode, and weirdly a digit shows up

Posted: Mon Jul 04, 2011 8:36 pm
by flying_circus
balikhan wrote:

Code: Select all

<pre><?php echo print_r(explode(" * ",$string1)); ?></pre><br />
You don't need to use echo before print_r.

print_r automatically prints the output of an array, but will return true if it succeeds. You're echoing the "true" value that is returned because print_r succeeded.

Re: Implode, then explode, and weirdly a digit shows up

Posted: Mon Jul 04, 2011 8:50 pm
by balikhan
flying_circus, you are a smart cookie. Thanks! :)