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

interpolation doesn't work for a variable in an array in an object

P: n/a
red
within an object function, this code:

$test2[]='test';
echo "$test[0]";

$this->test2[]='test2';
echo "$this->test2[0]\n";

produces this result:
test2
Array[0]

Why do I get Array[0] instead of the value in Array[0] ?
This only seems to happen when the variable is part of an array in an
object. If, for example, I want to wrap an object's variable in single
quotes this doesn't work:

"'$this->test2[0]'"

I can put double quotes areound the single quotes:

"'".$this->test2[0]."'"

but in a long string this becomes very tedious:

"$this->name=['".$this->mbox[0]."','".$this->mbox[1]."',".$this->mbox[2]."];";

is there any way to get "'$this->test2[0]'" to work ?

red


Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
red
The example I gave made no sense so I'll try again.

within an object function, this code:

$test[]='test';
echo "$test[0]";

$this->test2[]='test2';
echo "$this->test2[0]\n";

produces this result:
test
Array[0]

Why do I get Array[0] instead of the value in Array[0] ?
This only seems to happen when the variable is part of an array in an
object. If, for example, I want to wrap an object's variable in single
quotes this doesn't work:

"'$this->test2[0]'"

I can put double quotes areound the single quotes:

"'".$this->test2[0]."'"

but in a long string this becomes very tedious:

"$this->name=['".$this->mbox[0]."','".$this->mbox[1]."',".$this->mbox[2]."];";

is there any way to get "'$this->test2[0]'" to work ?

red

Jul 17 '05 #2

P: n/a
.oO(red)
within an object function, this code:

$test[]='test';
echo "$test[0]";

$this->test2[]='test2';
echo "$this->test2[0]\n";

produces this result:
test
Array[0]

Why do I get Array[0] instead of the value in Array[0] ?
The variable is part of the string, but for the PHP parser it ends after
'$this->test2', which produces the output 'Array'. It works with simple
variables like $test[0], but with more complex structures (like object
properties for example) the parser is not always able to detect which
chars still belong to the variable name and which don't.
This only seems to happen when the variable is part of an array in an
object. If, for example, I want to wrap an object's variable in single
quotes this doesn't work:

"'$this->test2[0]'"
Same as above.
I can put double quotes areound the single quotes:

"'".$this->test2[0]."'"

but in a long string this becomes very tedious:

"$this->name=['".$this->mbox[0]."','".$this->mbox[1]."',".$this->mbox[2]."];";

is there any way to get "'$this->test2[0]'" to work ?


Yep:

Complex (curly) syntax
<http://www.php.net/manual/en/language.types.string.php#language.types.string.pa rsing.complex>

Micha
Jul 17 '05 #3

P: n/a
Are you instantiating $this->test2 in the constructor?
i.e.

$this->test2 = array();
red <gr****@reenie.org> wrote in message news:<Ls******************@news.easynews.com>...
The example I gave made no sense so I'll try again.

within an object function, this code:

$test[]='test';
echo "$test[0]";

$this->test2[]='test2';
echo "$this->test2[0]\n";

produces this result:
test
Array[0]

Why do I get Array[0] instead of the value in Array[0] ?
This only seems to happen when the variable is part of an array in an
object. If, for example, I want to wrap an object's variable in single
quotes this doesn't work:

"'$this->test2[0]'"

I can put double quotes areound the single quotes:

"'".$this->test2[0]."'"

but in a long string this becomes very tedious:

"$this->name=['".$this->mbox[0]."','".$this->mbox[1]."',".$this->mbox[2]."];";

is there any way to get "'$this->test2[0]'" to work ?

red

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.