tags:

views:

60

answers:

3

In PHP we use....

foreach ($array as $value) {
    echo $value;
}

In Java the same thing can be done using

for (int e : array) {
    System.out.println(e);
}

is there any difference between the above 2 code segments

A: 

Php doesn't have generics.

fastcodejava
+1  A: 

This is very close to what both forms of java for-each iterator (numeric and Iterator) do, but PHP iterator also can provide keys and bind the iterated value by-reference, so you could modify it in place if you wanted to.

StasM
+1 for modify in place. That seems to be the main difference.
Thilo
A: 

I'm not very familiar with Java but your example suggests you need specify a data type for the index. PHP is loosely typed so you can use a foreach loop on associative and mixed arrays, not only pure numeric arrays. But, it's also possible to foreach your own custom objects just by implementing the Iterator interface.

Álvaro G. Vicario