Connecting Tech Pros Worldwide Forums | Help | Site Map

Object properties in echo()

JS Bangs
Guest
 
Posts: n/a
#1: Jul 16 '05
I started using PHP's object-oriented stuff a little while ago, which has
mostly been a joy. However, I've noticed that they don't seem to echo as I
would like. Eg:

$this->field['id'] = 255;
$this->key = 'id';
echo "$this->key is $this->field[$this->key]"; // prints "id is Array[id]"

// Thus I am forced to do this
$keyval = $this->field[$this->key];
echo "this->key is $keyval"; // prints "id is 255"
echo "this->key is " . $this->field[$this->key]; // also works

This example is trivial, but it becomes much more of a nuisance when I'm
making forms and whatnot and don't want to have to define a whole bunch of
new variables or keep opening and closing quotes. Is there a workaround or
different syntax I can use for this, or am I just stuck?

Jesse S. Bangs jaspax@u.washington.edu
http://students.washington.edu/jaspax/
http://students.washington.edu/jaspax/blog

Jesus asked them, "Who do you say that I am?"

And they answered, "You are the eschatological manifestation of the ground
of our being, the kerygma in which we find the ultimate meaning of our
interpersonal relationship."

And Jesus said, "What?"

André Nęss
Guest
 
Posts: n/a
#2: Jul 16 '05

re: Object properties in echo()


JS Bangs:
[color=blue]
> I started using PHP's object-oriented stuff a little while ago, which has
> mostly been a joy. However, I've noticed that they don't seem to echo as I
> would like. Eg:
>
> $this->field['id'] = 255;
> $this->key = 'id';
> echo "$this->key is $this->field[$this->key]"; // prints "id is Array[id]"
>
> // Thus I am forced to do this
> $keyval = $this->field[$this->key];
> echo "this->key is $keyval"; // prints "id is 255"
> echo "this->key is " . $this->field[$this->key]; // also works
>
> This example is trivial, but it becomes much more of a nuisance when I'm
> making forms and whatnot and don't want to have to define a whole bunch of
> new variables or keep opening and closing quotes. Is there a workaround or
> different syntax I can use for this, or am I just stuck?[/color]

Yeah, enclose them in {}, e.g.:
"{$this->key} is {$this->field[$this->key]}";

See:
http://www.php.net/manual/en/languag...string.parsing
and in particular:
http://www.php.net/manual/en/languag...arsing.complex

André Nęss
Closed Thread