Re: note 16451 added to function.eval
| From: | Zak Greant | Date: | Wed, 31 Oct 2001 11:11:11 +0000 |
| Subject: | Re: note 16451 added to function.eval | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-19698@lists.php.net to get a copy of this message | ||
On October 31, 2001 03:45 am, buddy@www.zabiny.cz wrote:
> Hello! I was asked how to solve this problem: <br>
> "How would I turn a string like "this string text blah" into 4
> different variables? e.g. $var1=this, $var2=string, etc. etc....?"
> <br>
> The solution is:
>
> <?
> $string = "this string text blah";
> $array = explode (" ", $string);
> for ($i = 0; $i < count($array); $i++)
> {
> $val = $array[$i];
> $j = $i+1;
> eval ("\$var$j = \$val;");
> }
> echo "$var1, $var2, $var3, $var4";
> ?>
> will print:
> this, string, text, blah
>
> Happy codding! buddy (Lukas Kyzlink)
Dear Lukas,
Thank you for the note. It would probably be better to do this without
using eval(). I would just use an array, but if you really want the
solution above, something like the code below should work.
<?php
$string = "this string text blah";
foreach (explode (' ', $string) as $key => $value) {
++$key;
${"var$key"} = $value;
}
?>
--
--zak
"We must be the change we wish to see." - M. K. Ghandi