Hello everybody,

I just started using drupal and programming in PHP. While analyzing the 'poll'-module I came across the language structure:

foreach ($node->choice as $choice) {
statement 1
statement 2
.
.
}

I searched the Internet (and some books on PHP) but was unable to find the meaning. Can somebody please help?

Regard,
Diederick Magré

Comments

kourge’s picture

foreach basically loops over an array.
For example:

$arr = array(1, 2, 3);
foreach ($arr as $x) {
  print $x . '\n';
}

You should get the output:

1
2
3
dmagre’s picture

Hi kourge,

Thanx for your answer, but I still have a question. What does the '->' mean?

Ik guess in your example code it would look like:

$arr = array(1, 2, 3);
foreach ($arr->array as $x) {
  print $x . '\n';
}

(I don't know how to use colours, sorry)

I added the '->array' after the $arr in the 'foreach'.

Can you help me with the '->'?

kourge’s picture

In PHP, arrays can be associative. That means you have a 'key' => 'value' relationship between array items, instead of just an ordered list of items. If you have an associative array like this:

$x = array(
  'a' => 1,
  'b' => 2,
  'c' => 3
);

Then accessing $x['a'] would give you 1.
In PHP, there's another similarly structured thing called objects. You can make an object from scratch, or you can simply "cast" an array into an object:

$x = array(
  'a' => 1,
  'b' => 2,
  'c' => 3
);

$x = (object)$x;

Now you access $x->a to get 1. In Drupal, node objects are made like this. They're casted from arrays to objects, which results in a single arrow syntax.
In Drupal's node_submit() function you can see that $node is first being casted as an object, in case it's still an associative array, then its properties (or "members") are accessed with the single arrow ayntax.

AjK’s picture

http://uk.php.net/manual/en/control-structures.foreach.php

I'm pretty stunned you searched the Internet and some PHP books without finding the meaning. This is one of the most used PHP constructs used on arrays in PHP.

When looking for PHP related stuff, it has a handy search feature in the top right of their homepage, start there first ;)

francis55’s picture

I'm not stunned at all!
The link you entered explains foreach, not ->
entering -> in the search box results in an error message

AjK’s picture

With regards to what -> means, that's a construct used to specify a property within an object.

These are pretty basic PHP language questions and not really Drupal questions. Your enquiries are probably better suited to a PHP forum rather than Drupal's forums.

dmagre’s picture

Thanks! I realise now that I can better have a look at the PHP site and buy a book on object oriented programming ;))

Thanx again

aoshi’s picture

>>buy a book on object oriented programming

actually, you don`t need one.
the only thing you needed to know about objects is the '->' operator. on the beginner`s level programming an object is just like an associative array, so, basically in most cases of simple web programming
$item->value == $item['value'];
for example when processing a SELECT query it is possible to write
$result = mysql_fetch_object($query_result) and use $result->value syntax afterwards
or
$result = mysql_fetch_assoc($query_result) and use $result['value']
the output will be just the same.
object-oriented programming is quite complex for a beginner and is almost totally unrelated to your subject.
so, advancing in basic PHP is much more useful for you for now, and you don`t really need a book, a CHM manual from php.net will be just fine.