By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,705 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,705 IT Pros & Developers. It's quick & easy.

Object properties in echo()

P: n/a
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 ja****@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?"
Jul 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
JS Bangs:
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?


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
Jul 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.