20 June 2005

PHP References and foreach

References in PHP are not like C pointers. I had bad feeling on this :(. In php 4, they have foreach construct. "Easy way to iterate over arrays", i.e. not easy to iterate over objects/references. It possible in php 5, refer here and need a litte tweak for php 4, (a user comment in the same page). Here an excerpt from one of my php source:
//Set red background for all options in a select field
$_opt =& $cat->_content;
foreach ($_opt as $k => $v) {
$v =& $_opt[$k];

$v->set_style('background-color:red');

unset($v);
}

No comments: