Advertisements
Advertisements
Question
Explain working of loops in array.
Answer in Brief
Advertisements
Solution
Using Foreach loop:
- The foreach statement is used to loop through arrays.
- For each pass, the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass, the next element will be processed.
Example:
<?php
$Student_name = arrayO’Magilan”, “Iniyan”, “Nilani” “Sibi” “Shini”); foreach ($Student_name as $value) { echo “$value <br>”;
}
?>
Using For Loop:
To loop through and print all the values of an indexed array.
Example:
<?php$cars = arrayC’Volvo”, “BMW”, “Toyota”);
$arrlength = count(cars);
for($x = 0; $x < $arrlength; $x++){
echo $cars[$x];
echo “<br>”;
}
?>shaalaa.com
Looping Structure
Is there an error in this question or solution?
APPEARS IN
RELATED QUESTIONS
Which loop evaluates condition expression as Boolean, if it is true, it executes statements and when it is false it will terminate?
PHP supports which types of looping techniques ______.
Consider the following code
<? php
$count=12;
do{
printf(“%d squared=%d<br/>”,
$count, pow($count,2));
} while($count<4);
?>
What will be the output of the code.
What will be the output of the following PHP code?
<?php
for ($x = 1; $x < 10;++$x)
{
print “*\t”;
}
?>What will be the output of the following PHP code?
<?php
for ($x = -1; $x < 10;--$x)
{
print $x;
}
?>Write Syntax of while loop in PHP.
Compare for loop and for each loop.
Write the purpose of Looping Structure in PHP.
Differentiate For each and While loop.
Explain the process Do while loop.
