By dmagre on
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
foreach
foreachbasically loops over an array.For example:
You should get the output:
And the '->'?
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:
(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 '->'?
->
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:Then accessing
$x['a']would give you1.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:
Now you access
$x->ato get1. 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$nodeis 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.Use the PHP manual
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 ;)
your link doesn't work
I'm not stunned at all!
The link you entered explains foreach, not ->
entering -> in the search box results in an error message
With regards to what ->
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.
Thanks
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
>>buy a book on object
>>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.