Explode.. returns the letters?

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
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Explode.. returns the letters?

Post by Steveo31 »

This is weird.. from the PHP manual, the following should return "foo" and "bar". Instead, it returns "f" and "o":

Code: Select all

<?php

$foo = "foo bar";
explode(" ", $foo);
echo $foo[0]."<br>".$foo[1];

?>
What am I doing wrong?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[php_man]explode[/php_man]() returns an array.

$foo must be an array.

Currently it's a string.
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post by Goowe »

http://www.disturbedfrenzy.com/disturbe ... ource=true

[I fixed it... lol, sorry for my stupid mistake Sami.]

Gotta use single quotes and set the explode() array to a variable....
Last edited by Goowe on Tue Mar 23, 2004 11:50 pm, edited 2 times in total.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

That code is wrong. Well it does the job, but it doesn't use explode(). Remove the explode line, and you will see the exact same result.

Steveo13: click on the the manual link to the [php_man]explode[/php_man]() page and view the examples + user notes.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Goowe wrote:http://www.disturbedfrenzy.com/disturbe ... ource=true

[I fixed it... lol, sorry for my stupid mistake Sami.]

Gotta use single quotes and set the explode() array to a variable....
heh

Nice to see you got it working. ;)
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Code: Select all

<?php 

$foo = "foo bar"; 
$exploded = explode(" ", $foo); 
echo $exploded[0]."<br>".$exploded[1]; 

?>
you need to assign the value returned from the function explode() to a variable, it does not automatically go back to the variable you used it on, $foo in you example still equals "foo bar"
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Sami wrote: Steveo13: click on the the manual link to the [php_man]explode[/php_man]() page and view the examples + user notes.
heh... did that last night and realized... doh.
Post Reply