user1122069
user1122069

Reputation: 1817

PHP - Array not looping through foreach

$array = Array(1,2,3);
foreach ($array as $identifier => $values_arr);
{
     echo(123);
}

The result is 123 instead of 123123123.

Upvotes: 0

Views: 52

Answers (2)

Samuel Cook
Samuel Cook

Reputation: 16828

What you have are actually two different segments of code.

The first:

foreach ($array as $identifier => $values_arr);

doesn't actually do anything, and is stopped.

And the second:

{echo (123);}

so the output is 123

to get into the foreach you will need to remove the semi-colon:

foreach ($array as $identifier => $values_arr){
     echo(123);
}

Upvotes: 4

user1122069
user1122069

Reputation: 1817

There is an extra colon on the end of the foreach line. The same issue often happens with if/else lines. I am posting this as I could not find the answer via Google search.

Upvotes: -1

Related Questions