ExamVeda
Login
Home
81
What will be the output of the following PHP code ?
<?php
$user = array("Ashley", "Bale", "Shrek", "Blank");
for ($x=0; $x < count($user) - 1; $x++)
{
    if ($user[$x++] == "Shrek") 
    continue;
    printf ($user[$x]); 
}
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
Only Bale is printed as $x++ is done before printing and then checked.
82
What will be the output of the following PHP code ?
<?php
$user = array("Ashley", "Bale", "Shrek", "Blank");
for ($x = 0; $x < count($user); $x) 
{
    if ($user[$x++] == "Shrek") 
    continue;
    printf ($user[$x]); 
}
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
x is incremented only inside loop i the if condition.
83
What will be the output of the following PHP code ?
<?php
for ($i = 0; $i % ++$i; $i++) 
{
    print"i";
}
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
Loop condition is true as i%(i+1) is a float non zero value in php.
84
What will be the output of the following PHP code ?
<?php
for ($i = 0; $i < 5; $i++) 
{
    for(; $i < 5; $i++)         
    print"i";
}
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
The i value is changed in the inner loop and reaches five, thus does not execute the second outer loop.
85
What will be the output of the following PHP code ?
<?php
$a = array("hi", "hello", "bye");
foreach ($a as $value) 
{
    if (count($a) == 2)
    print $value;         
}
?>
Discuss
Answer & Solution
Answer: Option D
Solution:
As count($a) returns 3 the condition is always false.
86
What will be the output of the following PHP code ?
<?php
$a = array("hi", "hello", "bye");
for (;count($a) < 5;) 
{
    if (count($a) == 3)
    print $a;               
}
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
As count($a) returns 3 the condition is always true, thus it prints $a, which returns its data type.