PHP For Loops
- What will be the output of the following PHP code ?
<?php
$stu = array("Ajit", "Ats", "Aju", "Rahul");
for ($g=0; $g < count($stu) - 1; $g++)
{
if ($stu[$g++] == "Ats")
continue;
printf ($stu[$g]);
}
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Only Bale is printed as $g++ is done before printing and then checked.
- What will be the output of the following PHP code ?
<?php
$emp = array("Neha", "Sumi", "Abhay", "Krishna");
for ($z = 0; $z < count($emp); $z)
{
if ($emp[$z++] == "Abhay")
continue;
printf ($emp[$z]);
}
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
z is incremented only inside loop z the if condition.
- What will be the output of the following PHP code ?
<?php
for ($g = 0; $g % ++$g; $g++)
{
print"g";
}
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Loop condition is true as g%(g+1) is a float non zero value in php.
- What will be the output of the following PHP code ?
<?php
for ($c = 4; $c < 9; $c++)
{
for(; $c < 9; $c++)
print"c";
}
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
The c value is changed in the inner loop and reaches five, thus does not execute the second outer loop.
- What will be the output of the following PHP code ?
<?php
$arr = array("Hello", "Interview", "Mania");
foreach ($arr as $val)
{
if (count($arr) == 2)
print $val;
}
?>
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
As count($arr) returns 3 the condition is always false.