循环语句与条件语句
This section contains Aptitude Questions and Answers on PHP Conditional Statements.
本节包含有关PHP条件语句的 Aptitude问题和解答。
If statements
If else statements
Switch statements
Loop statements
Options:
Correct answer: 3
A, B, and C
Explanation:
The IF, IF-ELSE, and SWITCH are conditional statements, while LOOPs are iterative statements in PHP.
如果陈述
如果是其他陈述
切换语句
循环语句
选项:
正确答案:3
A,B和C
说明:
IF,IF-ELSE和SWITCH是条件语句,而LOOP是PHP中的迭代语句。
<?php
$num = 20;
if ($num)
{
echo "Hello";
}
else
{
echo "Hii";
}
?>
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.
正确答案:1
你好
说明:
上面的代码将在网页上打印“ Hello” 。 因为变量$ num包含20,所以每个非零值都将视为PHP中if语句的真实条件。
<?php
if (echo("India "))
{
echo "Hello";
}
else
{
echo "Hii";
}
?>
Correct answer: 3
Error
Explanation:
The above code will generate syntax error. Because echo () function does not return any value.
<?php
if (print("India "))
{
echo "Hello";
}
else
{
echo "Hii";
}
?>
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.
正确答案:3
印度你好
说明:
代码正确,它将在网页上打印“ India Hello”。
print()函数返回在网页上打印的字符数,然后返回值将为6,然后IF条件为true。
<?php
if (true)
{
echo "Hello";
}
else
{
echo "Hii";
}
?>
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.
正确答案:1
你好
说明:
上面的代码将在网页上打印“ Hello”。 在这里,我们显式传递了true ,则if条件变为true。
<?php
if (0)
{
echo "Hello";
}
else
{
echo "Hii";
}
?>
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.
正确答案:2
i
说明:
上面的代码将在网页上打印“ Hii”。 因为在这里,我们在if条件中传递了0 ,所以我们知道0代表错误条件。
<?php
$num1 = 10;
$num2 = 20;
if ($num1 > $num2)
echo "ABC";
echo "PQR";
else
echo "XYZ";
?>
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.
<?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;
?>
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.
正确答案:3
30
说明:
上面的代码将在网页上打印“ 30”。 因为$ num1和$ num2分别包含10个和20个,分别小于$ num3 ,所以1st和2nd条件变为 false。 然后,其他部分被执行。
<?php
$num1 = 10;
$num2 = 20;
$num3 = 30;
if ($num1 > $num2 && $num1>$num3)
echo $num1;
elseif($num2 > $num1 && $num2>$num3)
echo $num2;
else
echo $num3;
?>
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.
正确答案:3
30
说明:
上面的代码将在网页上打印“ 30”。 因为$ num1和$ num2分别包含10个和20个,分别小于$ num3 ,所以1st和2nd条件变为 false。 然后,其他部分被执行。 我们可以将else if或elseif都用于条件语句。
<?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;
?>
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.
正确答案:3
30
说明:
上面的代码将在网页上打印“ 30”。 因为$ num1和$ num2分别包含10和20,所以它们小于$ num3 ,因此1st和2nd条件变为 false。 然后,其他部分被执行。 而且我们可以使用和或&&&两者来检查PHP中的多个条件。
<?php
if (10 and 20)
echo "ABC";
elseif(0 and -1)
echo "PQR";
else
echo "XYZ";
?>
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.
正确答案:1
美国广播公司
说明:
上面的代码将在网页上打印“ ABC”。 因为在if条件中,两个值10、20都不为零,这就是为什么条件变为true的原因。
<?php
if(0 and -1)
echo "ABC";
else if (10 and 20)
echo "PQR";
else
echo "XYZ";
?>
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.
正确答案:2
PQR
说明:
上面的代码将在网页上打印“ PQR”。 因为IF条件由于0而变为假, 否则因10和20都不为零而使条件成立。
<?php
if(print("ABC ") and print("PQR "))
echo "TUV";
else
echo "XYZ";
?>
Correct answer: 1
ABC PQR TUV
Explanation:
The above code will print "ABC PQR TUV" on the web page.
The both function returns a non-zero value, so that and operation of both is true.
<?php
if ( print ( "ABC " ) and print ( "PQR " ))
echo "TUV" ;
else
echo "XYZ" ;
?>
正确答案:1
ABC PQR TUV
说明:
上面的代码将在网页上打印“ ABC PQR TUV”。
两个函数都返回一个非零值,因此和运算都为true。
<?php
if (print ("ABC ") or print ("PQR ")) echo "TUV";
else echo "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".
正确答案:3
ABC TUV
说明:
上面的代码将在网页上打印“ ABC TUV”。 因为在或操作的情况下,如果第一个条件为true,则它不会检查第二个条件,因此它会打印“ ABC TUV”。
<?php
if (print ("ABC ") xor print ("PQR ")) echo "TUV";
else echo "XYZ";
?>
Correct answer: 2
ABC PQR XYZ
Explanation:
The XOR of 4 and 4 is 0, then the if condition gets false. So that it will print "ABC PQR XYZ".
<?php
if ( print ( "ABC " ) xor print ( "PQR " )) echo "TUV" ;
else echo "XYZ" ;
?>
正确答案:2
ABC PQR XYZ
说明:
4和4的XOR为0,则if条件变为false。 这样它将打印“ ABC PQR XYZ”。
<?php
if(print("ABC ") xor print('PQR '))
echo "TUV;
else
echo "XYZ";
?>
Correct answer: 4
Error
Explanation:
The above code will generate an error because the closing double quote is missing in the 3rd line.
<?php
if (true & print ("PQR ")) echo "TUV";
else echo "XYZ";
?>
Correct answer: 3
PQR TUV
<?php
if (true & true) if (true) echo "ABC";
else echo "PQR";
else echo "XYZ";
?>
Correct answer: 1
ABC
Explanation:
The "ABC" will be print because if(true & true) is true and then if(true) is also true.
<?php
if ( true & true ) if ( true ) echo "ABC" ;
else echo "PQR" ;
else echo "XYZ" ;
?>
正确答案:1
美国广播公司
说明:
将打印“ ABC”,因为if(true&true)为true,然后if(true)也为true。
<?php
switch (1)
{
case 0:
echo "ABC ";
case 1:
echo "PQR ";
case 2:
echo "TUV ";
default:
echo "XYZ ";
}
?>
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.
正确答案:2
PQR TUV XYZ
说明:
上面的代码将打印“ PQR TUV XYZ”。 因为这里我们没有使用break语句,所以将执行案例1 , 案例2和案例3 。
<?php
switch(1)
{
default:
echo "XYZ ";
break;
case 0:
echo "ABC ";
break;
case 1:
echo "PQR ";
break;
case 2:
echo "TUV ";
break;
}
?>
Correct answer: 1
PQR
Explanation:
The above will print "PQR" on the webpage, because here case 1 is the exact match.
<?php
switch ('1')
{
default:
echo "XYZ ";
break;
case 0:
echo "ABC ";
break;
case 1:
echo "PQR ";
break;
case 2:
echo "TUV ";
break;
}
?>
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.
正确答案:1
PQR
上面将在网页上打印“ PQR”,因为此处将执行情况1 ,因为1和“ 1”在开关中都将相同地工作。
<?php
switch ('1')
{
default:
echo "XYZ ";
break;
case "0":
echo "ABC ";
break;
case "1":
echo "PQR ";
break;
case "2":
echo "TUV ";
break;
}
?>
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.
正确答案:1
PQR
上面将在网页上打印“ PQR”,因为在此情况下将执行情况1 ,因为1,“ 1”和“ 1”在开关中的作用相同。
<?php
switch ('1')
{
default:
echo "XYZ ";
case 0:
echo "ABC ";
break;
case 1:
echo "PQR ";
break;
case 2:
echo "TUV ";
break;
}
?>
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".
正确答案:1
PQR
说明:
上面将在网页上打印“ PQR”,此处默认情况下缺少break语句,但在上面的代码中默认情况下将不执行,因此将打印“ PQR”。
<?php
switch(1)
case 1:
echo "PQR ";
break;
?>
Correct answer: 2
Error
Explanation:
The above code will generate syntax error, because we cannot use switch without curly brasses {}.
<?php
$num = 2;
switch (num)
{
case 0:
echo "ABC ";
break;
case 1:
echo "PQR ";
break;
case 2:
echo "TUV ";
break;
default:
echo "XYZ ";
break;
}
?>
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.
正确答案:1
美国广播公司
说明:
上面的代码将在网页上打印“ ABC”。 因为我们传递变量num而不是$ num,所以将num视为'0',然后执行情况0 。
翻译自: https://www.includehelp.com/php/conditional-statements-aptitude-questions-and-answers.aspx
循环语句与条件语句