Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
PHP Conditional Statements Aptitude Questions and Answers
PHP Conditional Statements Aptitude Questions and Answers: This section contains aptitude questions and answers on PHP Conditional Statements.
By Nidhi Last updated : December 15, 2023
This section contains Aptitude Questions and Answers on PHP Conditional Statements.
1) Which of the following statements are conditional statements in PHP?
- If statements
- If else statements
- Switch statements
- Loop statements
Options:
- A and B
- C
- A, B, and C
- A, B, C, and D
Correct answer: 3
A, B, and C
Explanation:
The IF, IF-ELSE, and switch are conditional statements, while LOOPs are iterative statements in PHP.
2) What is the correct output of the given code snippets?
<?php
$num = 20;
if ($num)
{
echo "Hello";
}
else
{
echo "Hii";
}
?>
- Hello
- Hi
- Error
- None of the above
Correct answer: 1
Hello
Explanation:
The above code will print "Hello" on the webpage. Because variable $num contains 20, then every non-zero value is treated as a true condition for if statements in PHP.
3) What is the correct output of the given code snippets?
<?php
if (echo("India "))
{
echo "Hello";
}
else
{
echo "Hii";
}
?>
- India Hello
- India Hii
- Error
- None of the above
Correct answer: 3
Error
Explanation:
The above code will generate syntax error. Because echo() function does not return any value.
4) What is the correct output of the given code snippets?
<?php
if (print("India "))
{
echo "Hello";
}
else
{
echo "Hii";
}
?>
- India Hello
- India Hii
- Error
- None of the above
Correct answer: 3
India Hello
Explanation:
The code is correct, it will print "India Hello" on the webpage.
The print() function returns the number of characters printed on the webpage, then the returned value will 6, then the IF condition will be true.
5) What is the correct output of the given code snippets?
<?php
if (true)
{
echo "Hello";
}
else
{
echo "Hii";
}
?>
- Hello
- Hii
- Error
- None of the above
Correct answer: 1
Hello
Explanation:
The above code will print "Hello" on the webpage. Here we explicitly passed true, then the if condition gets true.
6) What is the correct output of the given code snippets?
<?php
if (0)
{
echo "Hello";
}
else
{
echo "Hii";
}
?>
- Hello
- Hii
- Error
- None of the above
Correct answer: 2
Hii
Explanation:
The above code will print "Hii" on the webpage. Because here we pass 0 in the if condition, and we know that the 0 represents the false condition.
7) What is the correct output of the given code snippets?
<?php
$num1 = 10;
$num2 = 20;
if ($num1 > $num2)
echo "ABC";
echo "PQR";
else
echo "XYZ";
?>
- ABC PQR
- XYZ
- Error
- None of the above
Correct answer: 3
Error
Explanation:
The above code will generate a syntax error because the only single statement is allowed without curly brasses {} in the IF statement.
8) What is the correct output of the given code snippets?
<?php
$num1 = 10;
$num2 = 20;
$num3 = 30;
if ($num1 > $num2 && $num1>$num3)
echo $num1;
else if($num2 > $num1 && $num2>$num3)
echo $num2;
else
echo $num3;
?>
- 10
- 20
- 30
- Error
Correct answer: 3
30
Explanation:
The above code will print "30" on the webpage. Because $num1 and $num2 contains 10 and 20 respectively those are smaller than $num3, so that 1st and 2nd conditions get false. Then the else part gets executed.
9) What is the correct output of the given code snippets?
<?php
$num1 = 10;
$num2 = 20;
$num3 = 30;
if ($num1 > $num2 && $num1>$num3)
echo $num1;
elseif($num2 > $num1 && $num2>$num3)
echo $num2;
else
echo $num3;
?>
- 10
- 20
- 30
- Error
Correct answer: 3
30
Explanation:
The above code will print "30" on the webpage. Because $num1 and $num2 contains 10 and 20 respectively those are lower than $num3, so that 1st and 2nd conditions get false. Then the else part gets executed. And we can use else if or elseif both for conditional statements.
10) What is the correct output of the given code snippets?
<?php
$num1 = 10;
$num2 = 20;
$num3 = 30;
if ($num1 > $num2 and $num1>$num3)
echo $num1;
elseif($num2 > $num1 and $num2>$num3)
echo $num2;
else
echo $num3;
?>
- 10
- 20
- 30
- Error
Correct answer: 3
30
Explanation:
The above code will print "30" on the webpage. Because $num1 and $num2 contains 10 and 20 respectively, those are lower than $num3, so that 1st and 2nd conditions get false. Then the else part gets executed. And we can use and or && both for checking multiple conditions in PHP.
11) What is the correct output of the given code snippets?
<?php
if (10 and 20)
echo "ABC";
elseif(0 and -1)
echo "PQR";
else
echo "XYZ";
?>
- ABC
- PQR
- XYZ
- Error
Correct answer: 1
ABC
Explanation:
The above code will print "ABC" on the webpage. Because in the if condition both values 10, 20 are non-zero, that's why condition gets true.
12) What is the correct output of the given code snippets?
<?php
if(0 and -1)
echo "ABC";
else if (10 and 20)
echo "PQR";
else
echo "XYZ";
?>
- ABC
- PQR
- XYZ
- Error
Correct answer: 2
PQR
Explanation:
The above code will print the "PQR" on the webpage. Because IF condition gets false due to 0, and else if condition gets true because 10 and 20 both are non zero.
13) What is the correct output of the given code snippets?
<?php
if(print("ABC ") and print("PQR "))
echo "TUV";
else
echo "XYZ";
?>
- ABC PQR TUV
- ABC PQR XYZ
- ABC TUV
- ABC XYZ
Correct answer: 1
ABC PQR TUV
Explanation:
The above code will print "ABC PQR TUV" on the web page.
print("ABC ") return 4 and print "ABC ".
print("PQR ") return 4 and print "PQR ".
The both function returns a non-zero value, so that and operation of both is true.
14) What is the correct output of the given code snippets?
<?php
if (print ("ABC ") or print ("PQR ")) echo "TUV";
else echo "XYZ";
?>
- ABC PQR TUV
- ABC PQR XYZ
- ABC TUV
- ABC XYZ
Correct answer: 3
ABC TUV
Explanation:
The above code will print "ABC TUV" on the web page. Because in case of or operation, if the first condition is true then it does not check 2nd condition so that it prints "ABC TUV".
15) What is the correct output of the given code snippets?
<?php
if (print ("ABC ") xor print ("PQR ")) echo "TUV";
else echo "XYZ";
?>
- ABC PQR TUV
- ABC PQR XYZ
- ABC TUV
- Error
Correct answer: 2
ABC PQR XYZ
Explanation:
The print("ABC ") returns 4 and print "ABC ".
The print("PQR ") returns 4 and print "PQR ".
The XOR of 4 and 4 is 0, then the if condition gets false. So that it will print "ABC PQR XYZ".
16) What is the correct output of the given code snippets?
<?php
if(print("ABC ") xor print('PQR '))
echo "TUV;
else
echo "XYZ";
?>
- ABC PQR TUV
- ABC PQR XYZ
- ABC TUV
- Error
Correct answer: 4
Error
Explanation:
The above code will generate an error because the closing double quote is missing in the 3rd line.
17) What is the correct output of the given code snippets?
<?php
if (true & print ("PQR ")) echo "TUV";
else echo "XYZ";
?>
- PQR 1TUV
- PQR 0TUV
- PQR TUV
- None of the above
18) What is the correct output of the given code snippets?
<?php
if (true & true) if (true) echo "ABC";
else echo "PQR";
else echo "XYZ";
?>
- ABC
- PQR
- XYZ
- None of the above
Correct answer: 1
ABC
Explanation:
//Because below code is nested of "if(true & true)"
if(true)
echo "ABC";
else
echo "PQR";
The "ABC" will be print because if(true & true) is true and then if(true) is also true.
19) What is the correct output of the given code snippets?
<?php
switch (1)
{
case 0:
echo "ABC ";
case 1:
echo "PQR ";
case 2:
echo "TUV ";
default:
echo "XYZ ";
}
?>
- PQR
- PQR TUV XYZ
- Error
- None of the above
Correct answer: 2
PQR TUV XYZ
Explanation:
The above code will print the "PQR TUV XYZ". Because here we did not use break statement, then the case 1, case 2 and case 3 will be executed.
20) What is the correct output of the given code snippets?
<?php
switch(1)
{
default:
echo "XYZ ";
break;
case 0:
echo "ABC ";
break;
case 1:
echo "PQR ";
break;
case 2:
echo "TUV ";
break;
}
?>
- PQR
- PQR TUV XYZ
- Error
- None of the above
Correct answer: 1
PQR
Explanation:
The above will print "PQR" on the webpage, because here case 1 is the exact match.
21) What is the correct output of the given code snippets?
<?php
switch ('1')
{
default:
echo "XYZ ";
break;
case 0:
echo "ABC ";
break;
case 1:
echo "PQR ";
break;
case 2:
echo "TUV ";
break;
}
?>
- PQR
- PQR TUV XYZ
- Error
- None of the above
Correct answer: 1
PQR
The above will print "PQR" on the webpage, because here case 1 will be executed, because 1 and '1' will both work the same in the switch.
22) What is the correct output of the given code snippets?
<?php
switch ('1')
{
default:
echo "XYZ ";
break;
case "0":
echo "ABC ";
break;
case "1":
echo "PQR ";
break;
case "2":
echo "TUV ";
break;
}
?>
- PQR
- PQR TUV XYZ
- Error
- None of the above
Correct answer: 1
PQR
The above will print "PQR" on the webpage, because here case 1 will be executed, because 1, '1', and "1" will all work the same in the switch.
23) What is the correct output of the given code snippets?
<?php
switch ('1')
{
default:
echo "XYZ ";
case 0:
echo "ABC ";
break;
case 1:
echo "PQR ";
break;
case 2:
echo "TUV ";
break;
}
?>
- PQR
- XYZ PQR
- Error
- None of the above
Correct answer: 1
PQR
Explanation:
The above will print "PQR" on the webpage, here the break statement is missing in the default case, but in the above code default case will not execute, so that it will print "PQR".
24) What is the correct output of the given code snippets?
<?php
switch(1)
case 1:
echo "PQR ";
break;
?>
- PQR
- Error
Correct answer: 2
Error
Explanation:
The above code will generate syntax error, because we cannot use switch without curly brasses {}.
25) What is the correct output of the given code snippets?
<?php
$num = 2;
switch (num)
{
case 0:
echo "ABC ";
break;
case 1:
echo "PQR ";
break;
case 2:
echo "TUV ";
break;
default:
echo "XYZ ";
break;
}
?>
- ABC
- XYZ
- Error
- None of the above
Correct answer: 1
ABC
Explanation:
The above code will print "ABC" on the webpage. Because we pass variable num instead of $num then num is treated as '0' then case 0 is executed.