adspace


Explain the syntax for ‘foreach’ loop with example.

Answer Posted / Ravi Shankar Sharma

The foreach loop in PHP is used to iterate over arrays and objects. Here's an example:
- To iterate over an array:
$fruits = array('apple', 'banana', 'orange');
foreach ($fruits as $fruit) {
echo $fruit;
}
- To iterate over an associative array:
$person = array('name' => 'John', 'age' => 30);
foreach ($person as $key => $value) {
echo $key . ': ' . $value;
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Tell me what kind of things have you done on the social side?

1067


What is difference between static and final in php?

1109


if you run the app program all vendor open items are cleared but is it possible to reverse the again again open items please tell me the answer

1994


how to detect a mobile device using php

1174


What is the current stable version of php?

1126


Explain me is it possible to destroy a cookie?

1032


sort term descripttion form, report and uery

2254


Which variable declarations within a class is invalid in php?

1050


How to create a web form?

1113


How to calculate the difference between two dates using php?

1147


What is trait in php?

1068


Name and explain five of the PHP error constants?

1039


hello all, I need some sample placement papers in lion bridge.. can anyone help me?

2179


How can we extract string "pcds.co.in" from a string "https://info@pcds.co.in" using regular expression of php? More on reg can you explain

1080


Write a program using while loop?

1122