PDA

View Full Version : String concatination


Pompiuses
07-16-2002, 03:05 PM
I have this code:

for($i=1; $i<=6; $i++)
{
echo "<br>picture.$i._name";
}

The output of this is:
picture.1._name
picture.2._name
picture.3._name
picture.4._name
picture.5._name
picture.6._name

How can I get it to print this instead:
picture1_name
picture2_name
picture3_name
picture4_name
picture5_name
picture6_name

mordred
07-16-2002, 03:48 PM
Either


for($i=1; $i<=6; $i++)
{
echo "<br>picture${i}_name";
}


or


for($i=1; $i<=6; $i++)
{
echo "<br>picture{$i}_name";
}


or


for($i=1; $i<=6; $i++)
{
echo "<br>picture" . $i . "_name";
}


All should do the job. For reference, read the section about simple and complex syntax for variable parsing at http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing

Spookster
07-16-2002, 03:49 PM
for($i=1; $i<=6; $i++)
{
echo "<br>picture".$i."_name";
}

Pompiuses
07-17-2002, 10:07 AM
Can you tell me why this isn't working:

for($i=1; $i<=6; $i++)
{
if($this->picture".$i."_name == "")
continue;
}

Only get parse error in the if-test :mad:

firepages
07-17-2002, 10:18 AM
not enough info to go on there :)

is that code part of a class function ? where is $this coming from ?

the concat is wrong i.e. it should be


$this->picture.$i."_name";

but if you are saying that there is an object variable called eg
$this->picture1_name

then you may have to do something similar to.


for($i=1; $i<=6; $i++)
{
$temp=$this->picture.$i."_name";
echo ${$temp};
}

but without knowing what $this is is hard to say

Pompiuses
07-17-2002, 10:27 AM
i want to test if the objects $picture1_name is empty...
got a solution to it:

if($this->{"picture".$i."_name"} == "")
continue;

thanx anyway :)