Explain the syntax for ‘foreach’ loop with example.
Answer / 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 |
Does cors prevent csrf?
How to reset/destroy a cookie in php?
Why overriding is called runtime polymorphism?
What are php magic methods?
What are the 5 types of data?
Does winrunner 8.0 or qtp 8.0 supports php
How can I find the colour of a pixel of an image?
2 Answers Rushmore Consultancy,
Tell me what is use of in_array() function in php?
How many different types of messages available in php?
What is the difference between php4 and php5?
How do I find out the number of parameters passed into function?
Is it necessary to use closing tag in php?sometime without closing tag things work.why?