PHP Variables MCQ Questions and Answers

1. What symbol starts a variable in PHP?

a) @
b) $
c) %
d) #

Answer:

b) $

Explanation:

In PHP, a variable name starts with the $ symbol followed by the name of the variable.

2. Which of the following is a valid variable name in PHP?

a) $1variable
b) $_variable
c) $variable-1
d) $variable?

Answer:

b) $_variable

Explanation:

A valid PHP variable name starts with a letter or an underscore, followed by any number of letters, numbers, or underscores.

3. What is the correct way to declare a variable in PHP?

a) int $age = 25;
b) $age = 25;
c) $int age = 25;
d) var $age = 25;

Answer:

b) $age = 25;

Explanation:

In PHP, a variable is declared by the $ symbol followed by the variable name and an assignment.

4. How can a string be concatenated in PHP?

a) Using +
b) Using .
c) Using &
d) Using #

Answer:

b) Using .

Explanation:

In PHP, the concatenation operator is . which is used to append one string to another.

5. Which of the following is a way to declare a variable by reference in PHP?

a) $var1 = &$var2;
b) $var1 = $var2;
c) $var1 =& $var2;
d) $var1 == $var2;

Answer:

c) $var1 =& $var2;

Explanation:

In PHP, declaring a variable by reference is done by placing =& in the assignment.

6. How do you check if a variable is set in PHP?

a) isset($var)
b) set($var)
c) is_set($var)
d) $var is set

Answer:

a) isset($var)

Explanation:

The isset() function checks if a variable is set, which means it has been declared and is not null.

7. What is the output of echo in PHP when printing a Boolean value?

a) True or False
b) 1 for true, nothing for false
c) 0 for false, 1 for true
d) The word "Boolean"

Answer:

b) 1 for true, nothing for false

Explanation:

In PHP, when a Boolean is printed using echo, true outputs 1, and false results in no output.

8. What happens to a local variable in PHP when the function ends?

a) It continues to exist
b) It is reset to null
c) It is destroyed
d) It becomes a global variable

Answer:

c) It is destroyed

Explanation:

Local variables within a function in PHP are destroyed when the function execution is completed.

9. What does the 'global' keyword do in PHP?

a) Declares a global variable
b) Imports a global variable into a local function scope
c) Exports a local variable to the global scope
d) Converts a local variable to a global variable

Answer:

b) Imports a global variable into a local function scope

Explanation:

The 'global' keyword is used inside a function to access a variable defined in the global scope.

10. What data type does the gettype() function return in PHP?

a) String
b) Integer
c) Boolean
d) Array

Answer:

a) String

Explanation:

The gettype() function returns the data type of the variable as a string.

11. Which of the following is not a valid way to declare an array in PHP?

a) $array = [1, 2, 3];
b) $array = array(1, 2, 3);
c) $array = (1, 2, 3);
d) $array = array('1' => 1, '2' => 2, '3' => 3);

Answer:

c) $array = (1, 2, 3);

Explanation:

The correct ways to declare an array in PHP are either $array = [1, 2, 3]; or $array = array(1, 2, 3);.

12. What does the static keyword do to a variable within a function in PHP?

a) Makes the variable global
b) Preserves the variable's value for the next function call
c) Declares the variable as constant
d) Makes the variable available only within the function

Answer:

b) Preserves the variable's value for the next function call

Explanation:

The static keyword in a function's variable declaration means that the variable retains its value between function calls.

13. What is a superglobal variable in PHP?

a) A variable that overrides all other variables
b) A globally accessible variable defined by the PHP system
c) A variable that can store multiple data types
d) A variable declared at the beginning of the script

Answer:

b) A globally accessible variable defined by the PHP system

Explanation:

Superglobal variables in PHP are built-in variables that are always accessible, regardless of scope, and are used to store information about requests, sessions, etc.

14. How are variables passed to a function by default in PHP?

a) By value
b) By reference
c) By declaration
d) By copying

Answer:

a) By value

Explanation:

By default, variables are passed to functions by value, meaning a copy of the variable is made and used inside the function.

15. What is the correct way to declare a variable that should store a string in PHP?

a) string $var = "Hello";
b) $var = "Hello";
c) var string = "Hello";
d) $var: "Hello";

Answer:

b) $var = "Hello";

Explanation:

In PHP, a variable is declared by starting with the $ symbol, followed by the variable name, and an assignment. The data type is not explicitly declared.

16. Can variable names in PHP start with a number?

a) Yes
b) No
c) Only if followed by an underscore
d) Only in PHP 7 and above

Answer:

b) No

Explanation:

Variable names in PHP cannot start with a number. They must start with a letter or an underscore.

17. What is variable interpolation in PHP?

a) Swapping variables' values
b) Inserting variables into strings
c) Converting one variable type to another
d) Error handling in variables

Answer:

b) Inserting variables into strings

Explanation:

Variable interpolation in PHP is the process of embedding variable values within a string.

18. How do you check if a variable is an array in PHP?

a) is_array($var)
b) isarray($var)
c) $var is array
d) typeof($var) == "array"

Answer:

a) is_array($var)

Explanation:

The is_array() function checks whether a variable is an array or not in PHP.


Comments