Good evening or morning everyone..
Is there any way to understand this:
$array = ('element 1','element 2','element 3');
while($x=each($array)){
echo $x.' Value:'.$x;
}
I mean where did the 'key' and 'value' come from although they're not defined in the file at all??

Recommended Answers

All 8 Replies

Sorry, I've forgotten to enclose the code with coding tags.

I think php already defined it.. although I couldnt find that in php manual

I would use foreach in this case

I agree with evstevemd.

foreach ($array as $key => $value) {
  echo 'The key is '.$key.' and the value is '.$value; 
 }

$key and $value are created as part of the foreach statement.

See the manual page for each. It's in the third section (return values).

Thank you. I've found the solution.

I agree with evstevemd.

foreach ($array as $key => $value) {
  echo 'The key is '.$key.' and the value is '.$value; 
 }

$key and $value are created as part of the foreach statement.

Thank you, but that's not my question at all.

Thank you, but that's not my question at all.

Well the answer I provided is the same as the answer to your question. Perhaps I assumed you'd make the link. So, to be explicit:

I mean where did the 'key' and 'value' come from although they're not defined in the file at all??

'key' and 'value' are keys of the array $x which are created as part of the each() function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.