PHP Variables MCQs
PHP Variables MCQs : This section focuses on "Variable" in PHP. These Multiple Choice Questions (MCQ) should be practiced to improve the PHP skills required for various interviews (campus interview, walk-in interview, company interview), placements, entrance exams and other competitive examinations.
1. Any variables declared in PHP must begin with a _____?
A. .
B. #
C. &
D. $
View Answer
Ans : D
Explanation: Any variables declared in PHP must begin with a dollar sign ($)
2. A variable can have ________?
A. long descriptive names
B. short names
C. Both A and B
D. None of the above
View Answer
Ans : C
Explanation: A variable can have long descriptive names (like $factorial, $even_nos) or short names (like $n or $f or $x)
3. A variable name can only contain ____________?
A. alphanumeric characters
B. underscores
C. Both A and B
D. None of the above
View Answer
Ans : C
Explanation: A variable name can only contain alphanumeric characters and underscores in their name.
4. Variable names in PHP must start with ?
A. letter
B. underscore
C. no numbers
D. All of the above
View Answer
Ans : D
Explanation: One must keep in mind that variable names in PHP must start with a letter or underscore and should not start with
numbers.
5. PHP variables are case-sensitive?
A. True
B. False
C. For "sum" variable it is case-sensitive
D. None of the above
View Answer
Ans : A
Explanation: PHP variables are case-sensitive, i.e., $lfc and $LFC are treated differently.
6. How many variable scope are there in php?
A. 2
B. 3
C. 1
D. 4
View Answer
Ans : B
Explanation: Depending on the scopes, PHP has three variable scopes: Local, Global and static variable scope.
7. What will be the output of the following PHP code?
<?php
$x = 1;
$y = 2;
$z = "$x + $y";
echo "$z";
?<
A. $x + $y
B. 3
C. 1+2
D. 12
View Answer
Ans : C
Explanation: Variable z will store 1 + 2 because 1 + 2 is given in double-quotes.
8. What will be the output of the following PHP code?
<?php
$x = 3.3;
$y = 2;
echo $x % $y;
?>
A. 1.3
B. 1
C. 0
D. Error
View Answer
Ans : B
Explanation: % is the modulo operator. Unlike in C, we can use it to get a reminder or floating-point numbers in PHP.
9. What will be the output of the following PHP code?
<?php
$a = 1;
$b = 2;
$c = 3;
echo ($a % ($b) + $c);
?>
A. 2
B. 3
C. 4
D. 5
View Answer
Ans : A
Explanation: The innermost bracket is evaluated first, since it covers only variable b it is as good as not using brackets.
10. What will be the output of the following PHP code?
<?php
$a = 1;
$b = 2;
$c = 3;
echo ($a * (($b) - $c));
?>
A. 1
B. -1
C. 2
D. -2
View Answer
Ans : B
Explanation: The innermost bracket is evaluated first since it covers only variable B, it is as good as not using brackets, then $b- $c action will be performed.
11. Variables are ________ for storing information.
A. decimal
B. storage
C. numbers
D. containers
View Answer
Ans : D
Explanation: Variables are "containers" for storing information.
12. choose the incorrect variable name in php?
A. $_AGE
B. $18AGE
C. $AGE18
D. $_AGE18
View Answer
Ans : B
Explanation: $18AGE is the incorrect variable name in PHP. Because a variable name cannot start with a number.
13. A variable declared _____ has a GLOBAL SCOPE?
A. outside program
B. inside function
C. outside function
D. None Of the Above
View Answer
Ans : C
Explanation: A variable declared outside a function has a GLOBAL SCOPE.
14. A variable declared _____ has a LOCAL SCOPE?
A. outside program
B. inside function
C. outside function
D. None Of the Above
View Answer
Ans : B
Explanation: A variable declared inside a function has a LOCAL SCOPE.
15. Which of the following will be output of the following code?
<?php
function myTest() {
static $x;
echo $x;
}
myTest();
?>
A. 0
B. 1
C. No Output
D. Error
View Answer
Ans : C
Explanation: The output of the following code is No Output.
16. Which of the following is NOT a superglobal variable ?
A. $_SERVER
B. $_GLOBAL
C. $_GET
D. $_POST
View Answer
Ans : B
Explanation: $_GLOBAL is NOT a superglobal variable
17. Predict the output of the following code snippet :
<?php
$i=10;
function sample($i)
{
$i++;
echo $i.",";
}
echo $i.",";
GLOBAL $i;
sample($i);
echo $i;
?>
A. 11,11,11
B. 10,11,11
C. 10,11,10
D. 10,10,10
View Answer
Ans : C
Explanation: 10,11,10 the output of the following code snippet
18. Predict the output of the following code snippet :
<?php
$i=10;
function sample($i)
{
$i+=2;
GLOBAL $i;
echo $i+$i,",";
}
echo $i.",";
GLOBAL $i;
sample($i);
echo $i;
?>
A. 10,20,10
B. 10,24,10
C. 10,20,20
D. 10,22,10
View Answer
Ans : A
Explanation: 10,20,10 the output of the following code snippet
19. Predict the output of the following code snippet :
<?php
function sample()
{
$var1=20;
echo $var1;
echo $GLOBALS['var1'];
}
$var1=10;
sample();
echo $var1;
?>
A. 201010
B. 202010
C. 101010
D. 201020
View Answer
Ans : A
Explanation: 201010 the output of the following code snippet
20. what $_SERVER variable do?
A. Will give the information about all the predefined variables to know the server information
B. To get the information about the files uploaded using POST methods
C. When the information is passed using POST methods
D. None of the above
View Answer
Ans : A
Explanation: $_SERVER : Will give the information about all the predefined variables to know the server information
Also check :
Discussion