0

I am exploding an array appending it and then I am imploding the array back. Everything works fine but I got a small doubt. Please take the example below.

$x = "123,456,789"
explode (' ', $x);
$x[] = "987";
implode (',', $x);

The output looks like the following :

,123,456,789,987

The problem is that the comma appears before the values. I want them to appear after just like the following

123,456,789,987,
Srivathsan
  • 600
  • 3
  • 9
  • 19

2 Answers2

3

You're exploding on a blank space instead of a comma:

explode (' ', $x);

should be

explode (',', $x);
John Conde
  • 217,595
  • 99
  • 455
  • 496
0
$x = '123,456,789'; // use ' instead " (just for performance)
$x = explode (',', $x);
$x[] = '987';
$y = implode (',', $x);

echo $y . ','; // add trailing comma (what nickb said)

// output: 
123,456,789,987,

About the single vs double quotes issue please refers to PHP strings and Is there a performance benefit single quote vs double quote in php?

Community
  • 1
  • 1
Igor Parra
  • 10,214
  • 10
  • 69
  • 101
  • The performance difference between using single or double quotes for a string is beyond negligible. – nickb Jun 19 '12 at 17:55
  • @nickb Don't understand. So advice to a newbie to distinguish between correct uses of Single and Double quotes is not right any more? Please explain. – Igor Parra Jun 19 '12 at 18:24
  • There's realistically no performance benefit in switching between the two, nor is it "incorrect" to use double quotes when you don't have variables being substituted into the string. – nickb Jun 19 '12 at 19:23
  • @nickb I do not agree with you but whatever. When I do not need expand variables or some special characters I always use single quotes. For other readers interested in this issue just refers to [PHP strings](http://php.net/manual/en/language.types.string.php) – Igor Parra Jun 19 '12 at 19:44
  • Where on that page does it say "using single quotes will yield better performance over double quotes"? – nickb Jun 19 '12 at 19:52