Q. What will be the output of the following PHP code ?

Code:
<?php
$t = 5;
if ($t-- == ++$t)
{
    echo $t;
}
?>
  • (A) Error
  • (B) 0
  • (C) Nothing
  • (D) 5
πŸ’¬ Discuss
βœ… Correct Answer: (D) 5
Explanation: First $t = 5 is compared to and then decremented, then incremented and compared to $t = 5.

Q. What will be the output of the following PHP code ?

Code:
<?php
$s = 4;
$t = 8;
$s *= $t /= $s;
echo $s, $t;
?>
  • (A) 82
  • (B) 4
  • (C) 8
  • (D) Nothing
πŸ’¬ Discuss
βœ… Correct Answer: (A) 82
Explanation: Expression is evaluated from right to left.

Q. What will be the output of the following PHP code ?

Code:
<?php
$p = 5;
$q = 6;
if (++$p == $q++)
{
    echo "true ", $q, $p;
}
?>
  • (A) true
  • (B) 7
  • (C) 6
  • (D) true 76
πŸ’¬ Discuss
βœ… Correct Answer: (D) true 76
Explanation: p is preincremented and q is post incremented thus both are 6 in the if condition, later q is increment.

Q. What will be the output of the following PHP code ?

Code:
<?php
$str1 = "Hello ";
$str2 = "MCQ";
$str3 = "Buddy"; 
$str1 .= $str2 .= $str3 ;
echo $str1;
echo $str2;
?>
  • (A) Hello MCQ Buddy
  • (B) MCQ Buddy
  • (C) Hello MCQ Buddy MCQ Buddy
  • (D) MCQ Buddy MCQ Buddy
πŸ’¬ Discuss
βœ… Correct Answer: (C) Hello MCQ Buddy MCQ Buddy
Explanation: The str1 .= str2 is a shorthand for str1 = str1.str2 and this is evaluated from right to left.

Q. What will be the output of the following PHP code ?

Code:
<?php
$k = 15;
--$k;
echo $k++;
?>
  • (A) 15
  • (B) 14
  • (C) Error
  • (D) Nothing
πŸ’¬ Discuss
βœ… Correct Answer: (B) 14
Explanation: The + operator does union of arrays in that order, then the === operator compares key and value pairs.

Q. What will be the output of the following PHP code ?

Code:
<?php
$p = 12;
echo ++$p;
echo $p++;
echo $p;
echo ++$p;
?>
  • (A) Error
  • (B) 12
  • (C) Nothing
  • (D) 13131415
πŸ’¬ Discuss
βœ… Correct Answer: (D) 13131415
Explanation: ++$p increments a and then prints it,$p++ prints and then increments.

Q. What will be the output of the following PHP code ?

Code:
<?php
$s = 2.2;
switch($s)
{
case 2.1:
    print "Option A";
    break;
case 2.2:
    print "Option B";
    break;
default:
    print "Option C";
}
?>
  • (A) Error
  • (B) Option A
  • (C) Option B
  • (D) Option C
πŸ’¬ Discuss
βœ… Correct Answer: (C) Option B
Explanation: it compares it with 2.2 and thus prints Option B and exits.

Q. What will be the output of the following PHP code ?

Code:
<?php
const $k = 10;
switch($k)
{
case 10:
    print "First";
    break;
case 11:
    print "Second";
    break;
default:
    print "Third";
}
?>
  • (A) Third
  • (B) Second
  • (C) Error
  • (D) First
πŸ’¬ Discuss
βœ… Correct Answer: (C) Error
Explanation: Constants cannot be used in switch cases.

Q. What will be the output of the following PHP code ?

Code:
<?php
$k = 1;
switch(print $k)
{
case 2:
    print "Statement1";
    break;
case 1:
    print "Statement2";
    break;
default:
    print "Statement3";
}
?>
  • (A) Error
  • (B) Statement1
  • (C) Statement3
  • (D) 1Statement2
πŸ’¬ Discuss
βœ… Correct Answer: (D) 1Statement2
Explanation: Print returns 1,thus it gives case 1.

Q. What will be the output of the following PHP code ?

Code:
<?php
switch($t)
{
case 2:
    print "Hello";
    break;
case 1:
    print "World";
    break;
}
?>
  • (A) Error
  • (B) Hello
  • (C) World
  • (D) Hello world
πŸ’¬ Discuss
βœ… Correct Answer: (A) Error
Explanation: Error Undefined variable: t