Viktor Iwan
Viktor Iwan

Reputation: 89

Set Value on PHP Object Dynamic Properties

I have a php object, for example: album. Album has proporties img1, img2, img3

Now i use iteration(loop) for setting it, for example:

for($i=1;$i<3;$i++){
   $album->img{$i}="image".$i.".jpg";
}

However, the value cannot be set, so i wondering is there more appropriate approach to do this ?

Thanks

Upvotes: 1

Views: 81

Answers (1)

N3R4ZZuRR0
N3R4ZZuRR0

Reputation: 2412

Try this:

for($i=1;$i<3;$i++){
   $album->{"img".$i}="image".$i.".jpg";
}

Upvotes: 1

Related Questions