MCQs on Variables and Data Types | PHP Basics

Explore your understanding of PHP fundamentals with these multiple-choice questions, crafted to reinforce core concepts like variable declaration, data types, type casting, and constants. Perfect for beginners in PHP!


Variables and Data Types

Declaring Variables

  1. Which of the following is the correct way to declare a variable in PHP?
    • a) var myVariable = "Hello";
    • b) $myVariable = "Hello";
    • c) myVariable := "Hello";
    • d) int myVariable = "Hello";
  2. Which symbol is used to prefix a variable name in PHP?
    • a) %
    • b) #
    • c) @
    • d) $
  3. Which of the following statements about variables in PHP is correct?
    • a) Variable names can start with a number.
    • b) Variable names cannot contain special characters.
    • c) PHP variable names are case-insensitive.
    • d) PHP variable names must start with a letter or underscore.
  4. Which of the following is a valid PHP variable name?
    • a) $123name
    • b) $name@
    • c) $name
    • d) $-name

Data Types in PHP (String, Integer, Float, Boolean, Array, Object, NULL)

  1. Which data type is NOT a primitive type in PHP?
    • a) Boolean
    • b) Array
    • c) Integer
    • d) Object
  2. What is the data type of a variable $x = 5.6; in PHP?
    • a) Integer
    • b) Boolean
    • c) Float
    • d) String
  3. What will be the data type of $x = true; in PHP?
    • a) String
    • b) Integer
    • c) Boolean
    • d) Float
  4. Which of the following statements about PHP arrays is correct?
    • a) Arrays in PHP can only contain integers.
    • b) Arrays in PHP can store values of different data types.
    • c) Arrays in PHP can only store objects.
    • d) Arrays in PHP are limited to a fixed size.
  5. How do you define a NULL variable in PHP?
    • a) $var = 0;
    • b) $var = "";
    • c) $var = NULL;
    • d) $var = "null";
  6. Which data type does PHP automatically assign when a variable is assigned a textual value?
  • a) Boolean
  • b) Array
  • c) String
  • d) Integer
  1. If $x = "5" + 5;, what will be the resulting data type of $x?
  • a) Integer
  • b) String
  • c) Float
  • d) Boolean
  1. Which of the following data types can store decimal values?
  • a) Integer
  • b) Float
  • c) Boolean
  • d) Array
  1. How would you declare an empty object in PHP?
  • a) $obj = [];
  • b) $obj = new object();
  • c) $obj = new stdClass();
  • d) $obj = {};

Type Casting

  1. Which of the following correctly converts an integer to a float in PHP?
  • a) (int)$variable
  • b) (float)$variable
  • c) float($variable)
  • d) (integer)$variable
  1. If $var = "5.6", which of the following will convert $var to an integer?
  • a) (string)$var
  • b) (int)$var
  • c) (boolean)$var
  • d) (float)$var
  1. How can you convert a variable $num of type float to an integer in PHP?
  • a) $num = (float)$num;
  • b) $num = (int)$num;
  • c) $num = (string)$num;
  • d) $num = boolval($num);
  1. In PHP, which of the following is NOT a way to type cast a variable?
  • a) (int)$var
  • b) (boolean)$var
  • c) (array)$var
  • d) (char)$var
  1. What will be the result of the following PHP code: $val = (string)100;?
  • a) $val becomes an integer.
  • b) $val becomes a float.
  • c) $val becomes a string.
  • d) $val becomes an array.
  1. If $data = (bool)"false";, what will be the value of $data?
  • a) false
  • b) true
  • c) 0
  • d) 1
  1. Which function is used to get the data type of a variable in PHP?
  • a) type()
  • b) gettype()
  • c) datatype()
  • d) vartype()

Constants in PHP

  1. How do you define a constant in PHP?
  • a) $CONSTANT = "Value";
  • b) define("CONSTANT", "Value");
  • c) constant "CONSTANT" = "Value";
  • d) CONSTANT = "Value";
  1. Which of the following is TRUE about PHP constants?
  • a) They can be redefined.
  • b) They are case-sensitive by default.
  • c) They cannot be used inside functions.
  • d) They must start with a $ symbol.
  1. Which function can be used to check if a constant is already defined?
  • a) is_constant()
  • b) constant_exists()
  • c) defined()
  • d) isset()
  1. Which keyword is used in PHP to define a constant?
  • a) var
  • b) define
  • c) constant
  • d) const
  1. Once defined, how can you access a constant named MAX_VALUE in PHP?
  • a) $MAX_VALUE
  • b) MAX_VALUE
  • c) @MAX_VALUE
  • d) #MAX_VALUE
  1. Which of the following best describes a constant in PHP?
  • a) A variable that changes value frequently.
  • b) A value that cannot be changed once set.
  • c) A function parameter.
  • d) A temporary variable.
  1. How do you define a case-insensitive constant in PHP?
  • a) define("constant_name", "value", true);
  • b) define("constant_name", "value", false);
  • c) const constant_name = "value";
  • d) define_constant("constant_name", "value", true);
  1. Which of the following cannot be assigned to a constant in PHP?
  • a) Integer
  • b) Float
  • c) Array
  • d) String
  1. How do constants differ from variables in PHP?
  • a) Constants require the $ prefix.
  • b) Constants can be changed during runtime.
  • c) Constants do not need the $ prefix.
  • d) Constants are only accessible within functions.
  1. Which of the following is NOT true about constants in PHP?
  • a) They can be defined using the define() function.
  • b) They are automatically global across scripts.
  • c) They start with a $ symbol.
  • d) They cannot be redefined once set.

Answer Key

QnoAnswer
1b) $myVariable = "Hello";
2d) $
3d) PHP variable names must start with a letter or underscore.
4c) $name
5d) Object
6c) Float
7c) Boolean
8b) Arrays in PHP can store values of different data types.
9c) $var = NULL;
10c) String
11a) Integer
12b) Float
13c) $obj = new stdClass();
14b) (float)$variable
15b) (int)$var
16b) $num = (int)$num;
17d) (char)$var
18c) $val becomes a string.
19b) true
20b) gettype()
21b) define("CONSTANT", "Value");
22b) They are case-sensitive by default.
23c) defined()
24d) const
25b) MAX_VALUE
26b) A value that cannot be

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top