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
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";
Which symbol is used to prefix a variable name in PHP?
a) %
b) #
c) @
d) $
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.
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)
Which data type is NOT a primitive type in PHP?
a) Boolean
b) Array
c) Integer
d) Object
What is the data type of a variable $x = 5.6; in PHP?
a) Integer
b) Boolean
c) Float
d) String
What will be the data type of $x = true; in PHP?
a) String
b) Integer
c) Boolean
d) Float
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.
How do you define a NULL variable in PHP?
a) $var = 0;
b) $var = "";
c) $var = NULL;
d) $var = "null";
Which data type does PHP automatically assign when a variable is assigned a textual value?
a) Boolean
b) Array
c) String
d) Integer
If $x = "5" + 5;, what will be the resulting data type of $x?
a) Integer
b) String
c) Float
d) Boolean
Which of the following data types can store decimal values?
a) Integer
b) Float
c) Boolean
d) Array
How would you declare an empty object in PHP?
a) $obj = [];
b) $obj = new object();
c) $obj = new stdClass();
d) $obj = {};
Type Casting
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
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
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);
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
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.
If $data = (bool)"false";, what will be the value of $data?
a) false
b) true
c) 0
d) 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
How do you define a constant in PHP?
a) $CONSTANT = "Value";
b) define("CONSTANT", "Value");
c) constant "CONSTANT" = "Value";
d) CONSTANT = "Value";
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.
Which function can be used to check if a constant is already defined?
a) is_constant()
b) constant_exists()
c) defined()
d) isset()
Which keyword is used in PHP to define a constant?
a) var
b) define
c) constant
d) const
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
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.
How do you define a case-insensitive constant in PHP?