echo to php
am 14.08.2007 15:49:08 von Luca Cioria
I know it may seem odd, but is it possible to echo to php? I mean
neasting php within php?
example
$array_levels="[1][2][3]";
$my_array="hello world"
?>
It may seem stupid but would be powerful, wouldn't it?
How can I accomplish what I just did differently?
bye
Re: echo to php
am 14.08.2007 15:57:57 von burgermeister01
On Aug 14, 8:49 am, Luca Cioria wrote:
> I know it may seem odd, but is it possible to echo to php? I mean
> neasting php within php?
>
> example
>
>
>
> $array_levels="[1][2][3]";
>
> $my_array="hello world"
>
> ?>
>
> It may seem stupid but would be powerful, wouldn't it?
> How can I accomplish what I just did differently?
> bye
What you're talking about is the eval() command. This allows you to
execute a string as PHP. Although most programmers will tell you that
this is sloppy, bad programming. IMO eval() has its time and place,
but you should use it like a last resort. If I were you, I would try
and find a different solution first.
Re: echo to php
am 14.08.2007 16:05:51 von ELINTPimp
On Aug 14, 9:49 am, Luca Cioria wrote:
> I know it may seem odd, but is it possible to echo to php? I mean
> neasting php within php?
>
> example
>
>
>
> $array_levels="[1][2][3]";
>
> $my_array="hello world"
>
> ?>
>
> It may seem stupid but would be powerful, wouldn't it?
> How can I accomplish what I just did differently?
> bye
Luca,
Based on your two back-to-back posts to this forum, I am confused,
interested, and a little scared about what and how you are trying to
solve your problem =). If you care to share what you're trying to do,
we'll probably be able to help better with a little more information.
Re: echo to php
am 14.08.2007 16:18:38 von Michael Fesser
..oO(Luca Cioria)
>I know it may seem odd, but is it possible to echo to php? I mean
>neasting php within php?
eval()
But you don't want that, eval is evil. There's always another way.
In most cases a better algorithm will also be more efficient and faster
than eval().
>example
>
>
>
>$array_levels="[1][2][3]";
>
>$my_array="hello world"
>
>?>
>
>It may seem stupid but would be powerful, wouldn't it?
For a quick shot or a test - maybe, but not for production code.
>How can I accomplish what I just did differently?
Split your $array_levels (a different syntax like "1/2/3" might help),
then walk down the $my_array step by step in a loop or recursively.
I use the following method in my own framework to fetch a value from the
application's registry (configuration data and such things):
public final function getValue($key) {
$current = &$this->registry;
$subkey = strtok($key, '/');
while ($subkey) {
if (!isset($current[$subkey])) {
return NULL;
}
$current = &$current[$subkey];
$subkey = strtok('/');
}
return $current;
}
$this->registry is the nested array, $key is the path to the element I
want to fetch from the tree (in "1/2/3" syntax). The method just
traverses the tree by splitting the path with strtok() and loops through
the tree until the result is found.
Micha