On Thu, 05 May 2005 14:21:24 +0200, Marko wrote:
foreach($bars as $bar)
echo($bar.$title . $bar.$index . "<br>");
It prints Bar9 ten times. Why? How to get it to work properly?
Using php version 4.3.9. Thanks!
The "." is string concatenation, not member reference. That would be "->".
Also, you are assigning to the same variable bar. Your code will not work
because objects are references and you are always trying to use the same
object. That will set the whole array to the same value. You would
probably want something like this:
#!/usr/local/bin/php
<?php
class cls {
public $title = "";
public $index = "";
}
for ($i = 0;$i<10;$i++) {
$bar = new cls();
$bar->title = "Bar";
$bar->index = $i;
$bars[] = $bar;
}
foreach($bars as $bar) echo ($bar->title.$bar->index."\n");
?>
If I understood you correctly, this is what you want:
$ ./ttt.php
Bar0
Bar1
Bar2
Bar3
Bar4
Bar5
Bar6
Bar7
Bar8
Bar9
Unfortunately, I am no longer using PHP4 and I don't have it on the system.
--
Egoist: A person of low taste, more interested in themselves than in me.