Implode, then explode, and weirdly a digit shows up

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
balikhan
Forum Newbie
Posts: 2
Joined: Mon Jul 04, 2011 7:29 pm

Implode, then explode, and weirdly a digit shows up

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

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

Post 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.
balikhan
Forum Newbie
Posts: 2
Joined: Mon Jul 04, 2011 7:29 pm

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

Post by balikhan »

flying_circus, you are a smart cookie. Thanks! :)
Post Reply