echo to php

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I know it may seem odd, but is it possible to echo to php? I mean
neasting php within php?




$my_array<?php echo $array_levels; ?>="hello world"


It may seem stupid but would be powerful, wouldn't it?
How can I accomplish what I just did differently?

Re: echo to php

Quoted text here. Click to load it

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

Quoted text here. Click to load it


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

.oO(Luca Cioria)

Quoted text here. Click to load it


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().

Quoted text here. Click to load it

For a quick shot or a test - maybe, but not for production code.

Quoted text here. Click to load it

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.


Site Timeline