473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
3 1967
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
.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dave.harper | last post by:
I'm relativly new to C++, and have been looking for a way to interpolate data (i.e. a table indexed by 2 arrays, or just an array indexed by another array). I haven't been able to find an easy way...
14
by: Charles Banas | last post by:
I'm not sure if this is the right place to ask about this, but I've seen several posts in the past regarding Akima's Bivariate Interpolations routines, and i'm wondering if someone can give me some...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
6
by: Chris Simmons | last post by:
I know that a String is immutable, but I don't understand why this piece of code fails in nUnit: // BEGIN CODE using System; class Test { public static void Main( String args )
5
by: xandra | last post by:
i understood the concept of interpolation search. but i couldn't understand what would be the steps for that search. for example, if i'm searching for J in this file A A B E F H J M N N N N O P P...
5
by: different | last post by:
Hi, I have a program which reads a file containing integers in . The program reads the value of a variable every 2 seconds, then maps it to another interval, say , obtaining a new value. I already...
0
by: MonkeeSage | last post by:
There are several string interpolation functions, as well as string.Template. But here's yet another. This one emulates ruby's inline interpolation syntax (using #{}), which interpolates strings as...
10
by: John Passaniti | last post by:
(Note: This is not the same message I posted a week or so ago. The problem that prevented my previous attempt to work was a silly error in the template system I was using. This is a problem...
5
by: August Karlstrom | last post by:
Hi, Does anyone know the correct syntax to interpolate a class variable, $x say, inside a string? I tried "{self::$x}" but it produces the string {self::x}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.