Home »
MCQs
PHP Multiple-Choice Questions (MCQs)
PHP is a general-purpose server-side scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994. The PHP reference implementation is now produced by The PHP Group. PHP is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites.
This section contains the PHP MCQs on various topics such as Variables, Constants, Literals, Conditional & Control Statements, Arrays, Strings, Classes, etc.
These PHP MCQs are written for beginners as well as advanced, practice these MCQs to enhance and test the knowledge of PHP.
PHP MCQs Index
- PHP Basics MCQs
- PHP Variables, Variable Scopes MCQs
- PHP echo and print Statements MCQs
- PHP Data Types MCQs
- PHP Strings MCQs
- PHP Numbers MCQs
- PHP Math MCQs
- PHP Constants MCQs
- PHP Operators MCQs
- PHP Conditional Statements MCQs
- PHP Loops MCQs
- PHP Functions MCQs
- PHP Arrays MCQs
- PHP Superglobals MCQs
- PHP Regular Expressions MCQs
- PHP Date and Time MCQs
- PHP include and require Statements MCQs
- PHP File Handling MCQs
1) PHP Basics MCQs
1. PHP is an acronym for ____.
- Prefix Hypertext Preprocessor
- Prototype Hypertext Preprocessor
- Hypertext Preprocessor
- PHP: Hypertext Preprocessor
Answer: D) PHP: Hypertext Preprocessor
Explanation:
PHP is an acronym for "PHP: Hypertext Preprocessor". (Read: Introduction of PHP programming.)
Discuss this Question
2. Which is/are statement(s) true about PHP?
- It is an open-source scripting language
- PHP scripts execute on the server
- It is used for developing dynamic & interactive websites
- All of the above
Answer: D) All of the above
Explanation:
All of the above-mentioned statements are true about PHP. (Read: Advantages & Disadvantages of PHP)
Discuss this Question
3. What is the extension of a PHP file?
- .php
- .ph
- .phpfile
- All of the above
Answer: A) .php
Explanation:
The extension of a PHP file is .php.
Discuss this Question
4. Who developed PHP?
- Guido van Rossum
- Rasmus Lerdorf
- Jesse James Garrett
- Douglas Crockford
Answer: B) Rasmus Lerdorf
Explanation:
Rasmus Lerdorf developed PHP.
Discuss this Question
5. In which year PHP was developed?
- 1993
- 1994
- 1995
- 1996
Answer: B) 1994
Explanation:
PHP was developed in 1994.
Discuss this Question
6. A PHP script starts with ____ and ends with ___.
- <?php and ?>
- <php> and </php>
- <?php and /?php>
- </php and />
Answer: A) <?php and ?>
Explanation:
A PHP script starts with <?php and ends with ?>.
Read: Syntax of writing code in PHP
Discuss this Question
7. PHP keywords are case-sensitive?
- Yes
- No
Answer: B) No
Explanation:
No, PHP keywords (example: if, else, while, echo, etc.) are not case-sensitive.
Discuss this Question
8. Single line comments can be placed in PHP script by using which symbol?
- //
- #
- $
- Both A. and B.
Answer: D) Both A. and B.
Explanation:
In PHP, the single-line comment tells the interpreter to ignore everything that occurs on that line to the right of the comment. To place a single line comment type // or # and all text to the right will be ignored by the PHP interpreter.
Read: How to write comments in PHP?
Discuss this Question
9. Multi-line comments can be written within the ____.
- // and //
- ## and ##
- /* and */
- /// and ///
Answer: C) /* and */
Explanation:
In PHP, the multi-line comments can be written within the /* and */.
Discuss this Question
10. PHP statements end with a ______.
- semicolon (;)
- colon (:)
- dot (.)
- comma (,)
Answer: A) semicolon (;)
Explanation:
PHP statements end with a semicolon (;).
Discuss this Question
2) PHP Variables, Variable Scopes MCQs
11. Which sign is used to declare variables in PHP?
- @
- &
- $
- _
Answer: C) $
Explanation:
The dollar ($) sign is used to declare variables in PHP.
Read: PHP Variables
Discuss this Question
12. Which is not a valid variable name in PHP?
- age
- _age
- PersonAge
- 1age
Answer: D) 1age
Explanation:
The variable name 1age is not valid in PHP. A variable name must start with a letter or the underscore character.
Discuss this Question
13. Are variable names case-sensitive?
- Yes
- No
Answer: A) Yes
Explanation:
Yes, variable names are case-sensitive. Example: variables $name, $Name, and $NAME are different.
Discuss this Question
14. Which statement is commonly used for PHP output?
- write
- php.write
- log
- echo
Answer: D) echo
Explanation:
The PHP echo statement is commonly used for PHP output.
Discuss this Question
15. How many variable scopes are there in PHP?
- 1
- 2
- 3
- 4
Answer: C) 3
Explanation:
There are 3 different variable scopes in PHP. Those are
Discuss this Question
16. Which is not a valid variable scope in PHP?
- local
- global
- static
- external
Answer: D) external
Explanation:
The external is not a valid variable scope in PHP.
Discuss this Question
17. A variable declared outside a function has a ____.
- local scope
- global scope
Answer: B) global scope
Explanation:
A variable declared outside a function has a global scope.
Discuss this Question
18. What will be the output of the following PHP code?
<?php
$x = 5;
function myFunction(){
echo "Result $x";
}
myFunction();
?>
- Result $x
- Result 5
- Result
- None of the above
Answer: C) Result
Explanation:
The variable x is a global variable, and can only be accessed outside a function.
Discuss this Question
19. What will be the output of the following PHP code?
<?php
function myFunction()
{
$x = 5;
echo "Result1: $x , ";
}
myFunction();
echo "Result2: $x";
?>
- Result1: 5 , Result2:
- Result1: 5 , Result2: 0
- Result1: 5 , Result2: 5
- None of the above
Answer: A) Result1: 5 , Result2:
Explanation:
The variable x is a local scope, and can only be accessed within that function.
Discuss this Question
20. Which PHP keyword is used to access a global variable inside the function?
- php_ global
- global
- global_variable
- globalscope
Answer: B) global
Explanation:
PHP keyword global is used to access a global variable inside the function.
Discuss this Question
21. There are two variables a, b which declared in global scope, which is the correct PHP statement to access them within a function?
- global $a, $b;
- global $a $b;
- global ($a, $b);
- php_global $a, $b;
Answer: A) global $a, $b;
Explanation:
The correct PHP statement to access them within a function is: global $a, $b;
Discuss this Question
22. What is the name of an array that stores all global variables in PHP?
- $GLOBAL[]
- $global[]
- $GLOBALS[]
- $PHP_GLOBALS[]
Answer: C) $GLOBALS[]
Explanation:
PHP has an array named $GLOBALS[] that stores all global variables in PHP.
Discuss this Question
23. In the syntax of $GLOBALS[index], what does "index" hold?
- Index (starting from 0) of the variable
- Index (starting from 1) of the variable
- Name of the variable
- Line number of the variable where the variable was declared
Answer: C) Name of the variable
Explanation:
In the syntax of $GLOBALS[index], index holds the name of the variable.
Discuss this Question
24. What will be the output of the following PHP code?
<?php
function Increment(){
static $num = 0;
echo "$num";
$num++;
}
Increment();
Increment();
Increment();
?>
- 000
- 111
- 011
- 012
Answer: D) 012
Explanation:
In the above code, $num is a static variable and it will be initialized once. Thus, the output would be "012".
Discuss this Question
3) PHP echo and print Statements MCQs
25. Which statement is faster echo or print?
- echo
- print
Answer: A) echo
Explanation:
The echo statement is marginally faster than print statement.
Discuss this Question
26. What is the correct syntax of echo statement in PHP?
- echo
- echo()
- echo = ()
- Both A. and B.
Answer: D) Both A. and B.
Explanation:
The echo statement can be used with or without parentheses.
Discuss this Question
27. There is a variable "name" that contains the name of a person, which is/are the correct echo statement(s) to print the "name" suffix with "Hello".
- echo "Hello $name";
- echo "Hello " . $name;
- echo ("Hello $name");
- All of the above
Answer: D) All of the above
Explanation:
All of the above statements can be used to print the name suffix with "Hello".
Discuss this Question
28. What is the correct syntax of print statement in PHP?
- print
- print()
- print = ()
- Both A. and B.
Answer: D) Both A. and B.
Explanation:
The print statement can be used with or without parentheses.
Discuss this Question
29. There is a variable "name" that contains the name of a person, which is/are the correct print statement(s) to print the "name" suffix with "Hello".
- print "Hello $name";
- print "Hello " . $name;
- print ("Hello $name");
- All of the above
Answer: D) All of the above
Explanation:
All of the above statements can be used to print the name suffix with "Hello".
Discuss this Question
30. What will be the output of the following PHP code?
<?php
$a = 5;
$b = 10;
print ("$a, $b+10");
?>
- 5, 10+10
- 5, 20
- 5 10+10
- 5 20
Answer: A) 5, 10+10
Explanation:
In the above PHP script, the print statement prints the values of the variables and text as it as. Thus, in the statement "$a, $b+10", "+10" will be printed as it as.
Discuss this Question
31. What will be the output of the following PHP code?
<?php
var_dump (print "Hello");
?>
- Helloint(5)
- Helloint(6)
- Helloint(1)
- Hellonumber(5)
Answer: C) Helloint(1)
Explanation:
In the above PHP script, we used two statements var_dump() and print. The print statement prints the text and returns 1 always and its return type of print statement is int. The var_dump() prints the return type and value. Thus, first "Hello" will be printed and then "int(1)" will be printed. And, the final output will be "Helloint(1)".
Discuss this Question
4) PHP Data Types MCQs
32. Which data type is not supported by PHP?
- Integer
- Complex
- Float
- String
Answer: B) Complex
Explanation:
PHP does not support "Complex" data type.
Discuss this Question
33. String is placed within _____.
- Double quotes ("")
- Single quotes ('')
- Both A. and B.
- None of the above
Answer: C) Both A. and B.
Explanation:
String is written within either single quotes ('') or double quotes ("").
Discuss this Question
34. A PHP integer data type can store values between ____.
- -65,536 and 65,535
- 0 and 4,294,967,295
- -(2^63) and (2^63)-1
- -2,147,483,648 and 2,147,483,647
Answer: D) -2,147,483,648 and 2,147,483,647
Explanation:
An integer in PHP can store values between -2,147,483,648 and 2,147,483,647.
Discuss this Question
35. "Array" is a data type in PHP?
- Yes
- No
Answer: A) Yes
Explanation:
Yes, Array is a data type in PHP.
Discuss this Question
36. What will be the output of the following PHP code?
<?php
$x = true;
var_dump($x);
?>
- boolean('true')
- boolean(true)
- bool(true)
- bool('true')
Answer: C) bool(true)
Explanation:
The type of the true is bool. And, the var_dump() method returns the variable type along with the value. Thus, the output will be bool(true).
Discuss this Question
37. What will be the output of the following PHP code?
<?php
$Laptops = array(
"MAC",
"Lenovo",
"Dell",
"HP"
);
echo gettype($Laptops);
?>
- array
- array(4)
- object
- string
Answer: A) array
Explanation:
In the above PHP code, the type of the Laptops variable is an array and the function gettype() returns the type of the variable. Thus, the output will be array.
Discuss this Question
38. What is NULL in PHP?
- Value
- Keyword
- Data Type
- Function
Answer: C) Data Type
Explanation:
PHP introduced a special data type that is NULL which can have only one value: null.
Discuss this Question
39. What will be the output of the following PHP code?
<?php
$value1 = NULL;
$value2 = null;
var_dump($value1);
var_dump($value2);
?>
- Error
- NULL undefined
- NULL object
- NULL NULL
Answer: D) NULL NULL
Explanation:
Both of the values "NULL" and "null" are NULL. Thus, the output will be "NULL NULL".
Discuss this Question
40. Which type is used to store the database call?
- object
- class
- resource
- string
Answer: C) resource
Explanation:
In PHP, there is a special data type that is resource which is not an actual data type. The resource type of used to store the reference to functions and resources external to PHP.
Discuss this Question
5) PHP Strings MCQs
41. What will be the output of the following PHP code?
<?php
echo strpos("Hello, Includehelp!", "Includehelp!");
?>
- 6
- 7
- 8
- -1
Answer: B) 7
Explanation:
The strops() function is used to search for a text within a string and it returns the character position of the first match.
Discuss this Question
42. Which function is used to replace text within a string?
- str_replace()
- replace()
- replace_str()
- string-replace()
Answer: A) str_replace()
Explanation:
The str_replace() is used to replace text within a string.
Discuss this Question
43. What will be the output of the following PHP code?
<?php
$str = addcslashes("IncludeHelp","e");
echo($str);
?>
- Includehelp
- IncludeH\elp
- Includ\eHelp
- Includ\eH\elp
Answer: D) Includ\eH\elp
Explanation:
The addcslashes() function returns a string with backslashes in front of the specified characters.
Discuss this Question
44. What will be the output of the following PHP code?
<?php
$str = addslashes('Hello "Includehelp!"');
echo($str);
?>
- Hello Includehelp!
- Hello "Includehelp!"
- Hello ""Includehelp!""
- Hello \"Includehelp!\"
Answer: D) Hello \"Includehelp!\"
Explanation:
The addslashes() function is used to get a string with backslashes in front of predefined characters.
Discuss this Question
45. Which function converts a string of ASCII characters to hexadecimal values?
- bin2hex()
- str2hex()
- hex()
- str_hex()
Answer: A) bin2hex()
Explanation:
The bin2hex() function converts a string of ASCII characters to hexadecimal values.
Discuss this Question
46. What will be the output of the following PHP code?
<?php
$str = "Includehelp";
echo pack("H*",bin2hex($str));
?>
- 496e636c75646568656c70
- Includehelp
- 496e636c75646568656c70Includehelp
- Includehelp496e636c75646568656c70
Answer: B) Includehelp
Explanation:
The bin2hex() function converts a string of ASCII characters to hexadecimal values. And, the pack() function converts hexadecimal values back to the string.
Discuss this Question
47. Which function is used to convert the ASCII value to the character?
- asc()
- str()
- char()
- chr()
Answer: D) chr()
Explanation:
The chr() function is used to convert the given ASCII value to the character.
Discuss this Question
48. What is the complete syntax of chunk_split() function in PHP?
- chunk_split(string,length,end)
- chunk_split(string)
- chunk_split(string,end)
- chunk_split(length)
Answer: A) chunk_split(string,length,end)
Explanation:
The complete syntax of chunk_split() function is:
chunk_split(string,length,end)
Discuss this Question
49. Which function is used to convert hexadecimal values to the ASCII characters?
- hex2bin()
- hex2str()
- hex2asc()
- hex_string()
Answer: A) hex2bin()
Explanation:
The hex2bin() function is used to convert hexadecimal values to the ASCII characters.
Discuss this Question
50. What is the complete syntax of join() function in PHP?
- join(string1, string2)
- join(string1, string2, separator)
- join(separator, array)
- join(separator, string1, string2)
Answer: C) join(separator, array)
Explanation:
The complete syntax of join() function is:
join(separator, array)
Discuss this Question
51. There are two strings (in an array) "Hello" and "World", which is the correct PHP statement to join them with the separator ","?
- join(",","Hello","World")
- join("Hello","World",",")
- join(",", ("Hello","World"))
- join(",",array("Hello","World"))
Answer: D) join(",",array("Hello","World"))
Explanation:
The correct PHP statement to join two strings "Hello" and "World" is:
join(",",array("Hello","World"))
Discuss this Question
52. Which PHP function is used to print (output) a formatted string?
- echo()
- print()
- printf()
- prints()
Answer: C) printf()
Explanation:
The printf() function is used to print (output) a formatted string.
Discuss this Question
53. What will be the output of the following PHP code?
<?php
printf("%x",65535);
?>
- ffff
- 0xffff
- FFFF
- 0XFFFF
Answer: A) ffff
Explanation:
%x in printf() function prints/converts the number in hexadecimal number where letters are in lowercase.
Discuss this Question
54. Which PHP function is used to get the length of the string?
- strlength()
- strlen()
- length()
- str_len()
Answer: C) length()
Explanation:
The length() function is used to get the length of the string.
Discuss this Question
55. What will be the output of the following PHP code?
<?php
echo ucwords("hi, there how are you?");
?>
- Hi, there how are you?
- Hi, There How Are You?
- hi, there how are you?
- HI, THERE HOW ARE YOU?
Answer: B) Hi, There How Are You?
Explanation:
The ucwords() function is used to convert the first character of each word in a string to uppercase. Thus, the output will be "Hi, There How Are You?".
Discuss this Question
56. Which PHP functions are used to convert string to lowercase and uppercase?
- strupper() and strlower()
- str_toupper() and str_tolower()
- toupper() and tolower()
- strtoupper() and strtolower()
Answer: D) strtoupper() and strtolower()
Explanation:
The strtoupper() function is used to convert the string to uppercase. And, strtolower() function is used to convert the string to lowercase.
Discuss this Question
6) PHP Numbers MCQs
57. Which PHP constant returns the largest integer supported?
- INT_MAX
- MAX_INT
- MAXINT
- PHP_INT_MAX
Answer: D) PHP_INT_MAX
Explanation:
The PHP_INT_MAX is a PHP predefined constant which returns the largest integer supported.
Discuss this Question
58. Which PHP constant returns the size of an integer in bytes?
- INT_SIZE
- SIZE_INT
- INTSIZE
- PHP_INT_SIZE
Answer: D) PHP_INT_SIZE
Explanation:
The PHP_INT_SIZE is a PHP predefined constant which returns the size of an integer in bytes.
Discuss this Question
59. Which is/are the function(s) to check if the type of a variable is integer?
- is_int()
- is_integer()
- is_long()
- All of the above
Answer: D) All of the above
Explanation:
The all of the functions is_int(), is_integer(), and is_long() are used to check if the type of a variable is integer.
Discuss this Question
60. What will be the output of the following PHP code?
<?php
$x = -123.45;
echo var_dump(is_int($x));
?>
- bool(false)
- bool(true)
- false
- true
Answer: A) bool(false)
Explanation:
The is_int() method returns either true or false. Thus, the output of the above statement will be "bool(false)".
Discuss this Question
61. Which is/are the function(s) to check if the type of a variable is float?
- is_float()
- is_double()
- is_longfloat()
- Both A. and B.
Answer: D) Both A. and B.
Explanation:
The functions is_float() and is_double() are used to check if the type of a variable is float.
Discuss this Question
62. Which is/are the function(s) to check if a numeric value is finite or infinite?
- is_finite()
- is_infinite()
- is_inf()
- Both A. and B.
Answer: D) Both A. and B.
Explanation:
The functions is_finite() and is_infinite() are used to check if a numeric value is finite or infinite.
Discuss this Question
63. PHP NaN stands for ____.
- not-a-number
- not-a-numerical
- nothing-a-number
- numeric-a-number
Answer: A) not-a-number
Explanation:
PHP NaN stands for "not-a-number".
Discuss this Question
64. What will be the output of the following PHP code?
<?php
echo var_dump(is_nan(10));
?>
- bool(false)
- bool(true)
- false
- true
Answer: A) bool(false)
Explanation:
The is_nan() method is used to check whether the given value is NaN or not. Thus, the output of the above statement will be "bool(false)".
Discuss this Question
65. Which is the correct syntax to cast float to integer?
- (int) float_variable
- int (float_variable)
- (int) (float_variable)
- All of the above
Answer: A) (int) float_variable
Explanation:
The correct syntax to cast float to integer is:
(int) float_variable
Discuss this Question
7) PHP Math MCQs
66. Which PHP function is used to get the value of PI?
- php_pi()
- pivalue()
- pi()
- None of the above
Answer: C) pi()
Explanation:
The pi() function is used to get the value of PI.
Discuss this Question
67. Which PHP functions are used to find the lowest and highest values from a list of arguments?
- minimum() and maximum()
- min() and max()
- find_min() and find_max()
- getmin() and getmax()
Answer: B) min() and max()
Explanation:
The PHP functions min() and max() are used to find the lowest and highest values from a list of arguments.
Discuss this Question
68. Which PHP function is used to get the absolute value of the given number?
- abs()
- absolute()
- find_abs()
- php_abs()
Answer: A) abs()
Explanation:
The PHP function abs() is used to get the absolute value of the given number.
Discuss this Question
69. Which PHP function is used to get the square root of the given number?
- sqrt()
- squareroot()
- find_sqrt()
- php_sqrt()
Answer: A) sqrt()
Explanation:
The PHP function abs() is used to get the square root of the given number.
Discuss this Question
70. What will be the output of the following PHP code?
<?php
echo (round(0.49) . "," . round(0.50) . "," . round(0.51));
?>
- 0,0,0
- 1,1,1
- 0,0,1
- 0,1,1
Answer: D) 0,1,1
Explanation:
The round() function is used to round a floating-point number to its nearest integer. Thus, the output will be 0,1,1.
Discuss this Question
71. Which PHP function is used to get the random number?
- random()
- randomize()
- rand()
- randnumbers()
Answer: C) rand()
Explanation:
The PHP function rand() is used to get the random number.
Discuss this Question
72. Which PHP function is used to convert a number from one number base to another?
- convert()
- base_convert()
- parseInt()
- base_conversion()
Answer: B) base_convert()
Explanation:
The PHP function base_convert() is used to convert a number from one number base to another.
Discuss this Question
73. What is the correct syntax of PHP base_convert() function?
- base_convert(number, from_base, to_base);
- base_convert(number, to_base, from_base);
- base_convert(number);
- base_convert(number, to_base);
Answer: A) base_convert(number, from_base, to_base);
Explanation:
The correct syntax of PHP base_convert() function is:
base_convert(number, from_base, to_base);
Discuss this Question
74. What is the use of PHP mt_rand() function?
- Generates a random number using the milliseconds time.
- Generates a random number using the microseconds time.
- Generates a random number using the milliseconds time.
- Generates a random number using the Mersenne Twister algorithm.
Answer: D) Generates a random number using the Mersenne Twister algorithm
Explanation:
The use of the mt_rand() function in PHP is to generate a random number using the Mersenne Twister algorithm.
Discuss this Question
75. What is the use of PHP lcg_value() function?
- Generates a pseudo random number in a range between 0 and 1
- Generates a pseudo random number in a range between the given minimum to maximum numbers
- Generates a pseudo random number in a range between 0 and 1 using the Mersenne Twister algorithm
- Generates a pseudo random number in a range between -1 and +1
Answer: A) Generates a pseudo random number in a range between 0 and 1
Explanation:
The use of the lcg_value() function in PHP is to generate a pseudo random number in a range between 0 and 1.
Discuss this Question
8) PHP Constants MCQs
76. Which keyword/function is used to create a constant in PHP?
- define keyword
- define() function
- const keyword
- const() function
Answer: B) define() function
Explanation:
The define() function is used to create a constant in PHP.
Discuss this Question
77. Which is the correct syntax of define() function in PHP?
- define(constant_name)
- define(constant_name, value)
- define(constant_name, value, case-insensitive)
- define(constant_name = value)
Answer: C) define(constant_name, value, case-insensitive)
Explanation:
The correct syntax of the define() function is:
define(constant_name, value, case-insensitive)
Discuss this Question
78. What can be the value of case-insensitive parameter in define() function?
- true
- false
- Anything
- Both A. and B.
Answer: D) Both A. and B.
Explanation:
The case-insensitive parameter may have either true or false.
Discuss this Question
79. Give an example, how to define a constant array in PHP?
- const cities = new array(["New Delhi","Mumbai","Banglore"]);
- define("cities"=["New Delhi","Mumbai","Banglore"]);
- define("cities", ["New Delhi","Mumbai","Banglore"]);
- define("cities":["New Delhi","Mumbai","Banglore"]);
Answer: C) define("cities", ["New Delhi","Mumbai","Banglore"]);
Explanation:
The PHP define() function can also be used to create a constant array, here is an example:
define("cities", ["New Delhi","Mumbai","Banglore"]);
Discuss this Question
80. What is the scope of a constant in PHP?
- Local
- Global
- Static
- Fixed
Answer: B) Global
Explanation:
The constants are automatically global and can be used across the entire script.
Discuss this Question
9) PHP Operators MCQs
81. What is name of PHP "===" operator?
- Equal
- Safe Equal
- Identity
- Identical
Answer: D) Identical
Explanation:
The PHP "===" operator is known as the "Identical" operator and it is used to compare two operands and returns true if they are of the same type.
Discuss this Question
82. What will be the output of the following PHP code?
<?php
$a = 123;
$b = "123";
var_dump($a !== $b);
?>
- Error
- bool(false)
- bool(true)
- true
Answer: C) bool(true)
Explanation:
The statement $a !== $b will return true because the types of $a and $b are not equal. And, the var_dump() function will return the type and value of the expression. Thus, the output will be bool(true).
Discuss this Question
83. What is name of PHP "<=>" operator?
- Spaceship
- Safe Spaceship
- Identity
- Identical
Answer: A) Spaceship
Explanation:
The PHP <=> operator is known as the "Spaceship" operator. It returns an integer value that is less than, equal to, or greater than zero, depending on if expression is less than, equal to, or greater than $y. The Spaceship Operator (<=>) was introduced in PHP 7.
Discuss this Question
84. Does PHP support Ternary Operator?
- Yes
- No
Answer: A) Yes
Explanation:
Yes, PHP supports Ternary Operator. The operator ?: is used as a Ternary Operator.
Discuss this Question
85. Which PHP operator known as "Null coalescing" operator?
- ?
- ?=>
- ??
- ???
Answer: C) ??
Explanation:
The PHP operator ?? is known as "Null coalescing" operator. The syntax is:
$value = expression1 ?? expression2
It returns the value of $value. The value of $value is expression1 if expression1 exists, and is not NULL. If expression1 does not exist, or is NULL, the value of $value is expression2.
The Null coalescing operator (??) was introduced in PHP 7.
Discuss this Question
10) PHP Conditional Statements MCQs
86. What will be the output of the following PHP code?
<?php
$x = 100;
if ($x < 200){
echo "True";
}
else{
echo "False";
}
?>
- True
- False
Answer: A) True
Explanation:
In the above PHP script, the expression is true. Thus, the output will be "True".
Discuss this Question
87. What will be the output of the following PHP code?
<?php
$a = 15;
$b = 20;
if ($a < ++$a || $b < ++$b){
echo "True";
}
else{
echo "False";
}
?>
- True
- False
- TrueFalse
- Parse Error
Answer: B) False
Explanation:
In PHP, the precedence of ++ operator is higher than < operator, so the first increment operation executes, and then the comparison operation will execute. But when the comparison operation executes, both OR operator conditions will return false. Thus, the else block will be executed.
Discuss this Question
88. Which PHP statement is used to select one of many blocks of code to be executed?
- The if statement
- The if...else statement
- The switch statement
- The select statement
Answer: C) The switch statement
Explanation:
The switch statement is used to select one of many blocks of code to be executed.
Discuss this Question
89. In PHP, the break statements with case blocks are optional?
- Yes
- No
Answer: A) Yes
Explanation:
Yes, the break statements with case blocks are optional.
Discuss this Question
90. What will be the output of the following PHP code?
<?php
$car = "Honda";
switch ($car) {
case "Honda":
echo "You selected Honda.";
case "BMW":
echo "You selected BMW.";
case "AUDI":
echo "You selected Audi.";
default:
echo "None is selected.";
}
?>
- You selected Honda.
- You selected Honda.You selected BMW.
- You selected Honda.You selected BMW.You selected Audi.
- You selected Honda.You selected BMW.You selected Audi.None is selected.
Answer: D) You selected Honda.You selected BMW.You selected Audi.None is selected.
Explanation:
In the above PHP Script, break statements were not used in the case blocks. The value of $car is "Honda", so the first case executes and then all other blocks will be executed because the switch statement executes the blocks from the matched case block to the bottom if the break statement is not there.
Discuss this Question
11) PHP Loops MCQs
91. How many loops are there in PHP?
- 2
- 3
- 4
- 5
Answer: C) 4
Explanation:
There are 4 types of Loops are there in PHP which are while, do...while, for, and foreach.
Discuss this Question
92. What will be the output of the following PHP code?
<?php
$counter = 1;
while ($counter++ <= 5)
{
echo $counter, ",";
$counter++;
}
?>
- 2,4,
- 1,2,3,4,5,
- 2,4,6,
- 1,2,4,
Answer: C) 2,4,6,
Explanation:
In the while expression, the statement $counter++ is a post-increment operation and it executes after checking the condition. Thus, the output will be "2,4,6,".
Discuss this Question
93. Which loop statement is used to loop through a block of code a specified number of times?
- while
- do...while
- for
- foreach
Answer: C) for
Explanation:
In PHP, the for loop statement is used to loop through a block of code a specified number of times.
Discuss this Question
94. Which loop statement is used to loop through a block of code for each element in an array?
- while
- do...while
- for
- foreach
Answer: D) foreach
Explanation:
In PHP, the foreach loop statement is used to loop through a block of code for each element in an array.
Discuss this Question
95. What will be the output of the following PHP code?
<?php
$cars = array(
"BMW",
"Mercedes",
"Honda",
);
foreach ($cars as $c)
{
echo "$c ";
}
?>
- BMW Mercedes Honda
- Honda Mercedes BMW
- SyntaxError
- TypeError
Answer: A) BMW Mercedes Honda
Explanation:
The foreach loop statement is used to loop through a block of code for each element in an array. Thus, the output will be "BMW Mercedes Honda".
Discuss this Question
96. Which PHP statement is used to jump out of a loop?
- exit
- break
- continue
- stop
Answer: B) break
Explanation:
In PHP, the break statement is used to jump out of a loop.
Discuss this Question
97. What is the use of PHP "continue" statement?
- breaks the loop and transfers the control to the statement written just after the loop body
- breaks the all loop statement (outer loops and inner loops)
- breaks one iteration of the loop and transfers the control to the next loop iteration
- All of the above
Answer: C) breaks one iteration of the loop and transfers the control to the next loop iteration
Explanation:
In PHP, the continue statement is used to break one iteration of the loop and transfer the control to the next loop iteration.
Discuss this Question
98. What will be the output of the following PHP code?
<?php
for ($i = 1;$i <= 10;$i++){
if ($i == 6){
continue;
}
echo "$i ";
}
?>
- 1 2 3 4 5 7 8 9 10
- 1 2 3 4 5 6 7 8 9 10
- 1 2 3 4 5 6
- 1 2 3 4 5
Answer: A) 1 2 3 4 5 7 8 9 10
Explanation:
In the above PHP script, we used the continue statement when $i==6, the continue statement does not echo the value of $i when the value of $i is 6 and transfer the control again to the loop. Thus. The output will be "1 2 3 4 5 7 8 9 10".
Discuss this Question
99. The "break" statement is used in switch and loop statements, can the "continue" statement be used in the switch statement instead of the "break" statement?
- Yes
- No
Answer: B) No
Explanation:
No, we cannot use the continue statement instead of the break statement in the switch statement.
Discuss this Question
100. What will be the output of the following PHP code?
<?php
$x = 10;
while ($x < 10) {
$x++;
echo $x, ",";
}
?>
- Infinite loop
- Error
- 10, 11,
- No output
Answer: D) No output
Explanation:
In the above PHP script, the condition which is written in the while statement is false. Thus, the loop will not be executed and nothing will print.
Discuss this Question
12) PHP Functions MCQs
101. Which keyword is used to define a function in PHP?
- def
- fun
- func
- function
Answer: D) function
Explanation:
The function keyword is used to define a function in PHP.
Discuss this Question
102. The PHP function names are case-sensitive?
- Yes
- No
Answer: B) No
Explanation:
No, the PHP function names are not case-sensitive.
Discuss this Question
103. What will be the output of the following PHP code?
<?php declare(strict_types = 1);
function details(string $name, int $age){
echo "Name: $name, Age: $age";
}
details("Alvin", "21");
?>
- Name: Alvin, Age: 21
- Name: Alvin, Age:
- Name: Alvin, Age: 0
- Fatal Error
Answer: D) Fatal Error
Explanation:
In the PHP Script, we are using the strict declaration, it will return a Fatal Error if the datatype of the function definition and calling mismatches. Thus, the output will be "Fatal Error".
The output is:
PHP Fatal error: Uncaught TypeError: Argument 2 passed to details() must be of the type integer,
string given, called in /home/fBGU1n/prog.php on line 6 and defined in
/home/fBGU1n/prog.php:3 Stack trace: #0 /home/fBGU1n/prog.php(6): details('Alvin', '21') #1 {main} thrown
in /home/fBGU1n/prog.php on line 3
Discuss this Question
104. Which is the correct syntax of defining a default argument value in PHP?
- function function_name(type $argument_name : value) { /* function body*/ }
- function function_name(type $argument_name , value) { /* function body*/ }
- function function_name(type $argument_name = value) { /* function body*/ }
- All of the above
Answer: C) function function_name(type $argument_name = value) { /* function body*/ }
Explanation:
The syntax of defining a default argument value in PHP is:
function function_name(type $argument_name = value) {
/* function body*/
}
Discuss this Question
105. Which is the correct syntax of defining function return type in PHP?
- function function_name(type $arguments) : return_type { /* function body*/ }
- function function_name(type $arguments) , return_type { /* function body*/ }
- return_type function function_name(type $arguments) { /* function body*/ }
- function return_type function_name(type $arguments) { /* function body*/ }
Answer: A) function function_name(type $arguments) : return_type { /* function body*/ }
Explanation:
The syntax of defining function return type in PHP is:
function function_name(type $arguments) : return_type {
/* function body*/
}
Discuss this Question
13) PHP Arrays MCQs
106. Which PHP function is used to create an array?
- array()
- arr()
- new_array()
- array()
Answer: A) array()
Explanation:
The PHP array() function is used to create an array.
Discuss this Question
107. How many types of arrays are there in PHP?
- 1
- 2
- 3
- 4
Answer: C) 3
Explanation:
There are 3 types of arrays are there in PHP.
Discuss this Question
108. Which is/are valid types of arrays in PHP?
- Indexed arrays
- Associative arrays
- Multidimensional arrays
- All of the above
Answer: D) All of the above
Explanation:
All of the above types are valid arrays in PHP.
Discuss this Question
109. What is the difference between Indexed array and Associative array in PHP?
- Index array has numeric index while associative array has named keys
- Index array has numeric index while associative array has named keys and numeric index both
- Index array is one-dimensional array with numeric index while associative array is two-dimensional array with numeric index
- Index array has numeric index while associative array has one or more arrays
Answer: A) Index array has numeric index while associative array has named keys
Explanation:
The main difference between Indexed array and Associative Array is that - Index array has numeric index while associative array has named keys.
Discuss this Question
110. Which is the correct example of an Indexed array in PHP?
- $cities = array("Delhi"; "Mumbai"; "Banglore");
- $cities = array("Delhi", "Mumbai", "Banglore");
- $cities = new array("Delhi", "Mumbai", "Banglore");
- $cities = new array(3) ("Delhi", "Mumbai", "Banglore");
Answer: B) $cities = array("Delhi", "Mumbai", "Banglore");
Explanation:
The valid example of an Indexed array in PHP is:
$cities = array("Delhi", "Mumbai", "Banglore");
Discuss this Question
111. Which is the correct example of an Associative array in PHP?
- $person = array("Alvin"=>"Delhi", "Alex"=>"Mumbai", "Bhavik"=>"Banglore");
- $person = array("Alvin"=>"Delhi"; "Alex"=>"Mumbai"; "Bhavik"=>"Banglore");
- $person = new array("Alvin"=>"Delhi", "Alex"=>"Mumbai", "Bhavik"=>"Banglore");
- $person = new array("Alvin"=>"Delhi"; "Alex"=>"Mumbai"; "Bhavik"=>"Banglore");
Answer: A) $person = array("Alvin"=>"Delhi", "Alex"=>"Mumbai", "Bhavik"=>"Banglore");
Explanation:
The valid example of an Associative array in PHP is:
$person = array("Alvin"=>"Delhi", "Alex"=>"Mumbai", "Bhavik"=>"Banglore");
Discuss this Question
112. Which PHP function(s) is/are used to compare arrays and returns the differences?
- array_diff()
- array_diff_assoc()
- array_diff_key()
- All of the above
Answer: D) All of the above
Explanation:
All of the above PHP functions are used to compare arrays and returns the differences.
Discuss this Question
113. What is the difference between array_diff() and array_diff_assoc() functions?
- array_diff() compares the values only while array_diff_assoc() compares the keys only
- array_diff() compares the values only while array_diff_assoc() compares the keys and values
- Both functions can be used to compare the values and keys
- None of the above
Answer: B) array_diff() compares the values only while array_diff_assoc() compares the keys and values
Explanation:
The difference between array_diff() and array_diff_assoc() functions is: The array_diff() compares the values only while array_diff_assoc() compares the keys and values.
Discuss this Question
114. Which PHP function is used to sort multi-dimensional arrays?
- array_sort()
- sort()
- multisort()
- array_multisort()
Answer: D) array_multisort()
Explanation:
The PHP function array_multisort() is used to sort multi-dimensional arrays.
Discuss this Question
115. What is the use of PHP sort() function?
- Sorts an indexed array
- Sorts an associative array
- Sorts a multi-dimensional array
- Sorts any kind of array
Answer: A) Sorts an indexed array
Explanation:
The PHP function sort() is used to sort an indexed array.
Discuss this Question
116. Which PHP function is used to sort an indexed array in descending order?
- sort_reverse()
- reverse_sort()
- revsort()
- rsort()
Answer: D) rsort()
Explanation:
The PHP function rsort() is used to sort an indexed array in descending order.
Discuss this Question
117. Which PHP function is used to return the current element in an array?
- get()
- start()
- current()
- cur()
Answer: C) current()
Explanation:
The PHP function current() is used to return the current element in an array.
Discuss this Question
118. Which PHP function is used to check if a specified value exists in an array?
- in_array()
- check_array()
- exist()
- None of the above
Answer: A) in_array()
Explanation:
The PHP function in_array() is used to check if a specified value exists in an array.
Discuss this Question
119. Which PHP function is used to sort an associative array in descending order, according to the key?
- sort()
- rsort()
- ksort()
- krsort()
Answer: D) krsort()
Explanation:
The PHP function krsort() is used to sort an associative array in descending order, according to the key.
Discuss this Question
120. Which PHP function is used to get one or more random keys from an array?
- array_random()
- array_randomize()
- array_rand()
- krand()
Answer: C) array_rand()
Explanation:
The PHP function array_rand() is used to get one or more random keys from an array.
Discuss this Question
14) PHP Superglobals MCQs
121. Which is not a valid PHP superglobal variable?
- $GLOBALS
- $_SERVER
- $_ENV
- $_SESSIONS
Answer: D) $_SESSIONS
Explanation:
$_SESSIONS is not a PHP Superglobal variable.
Discuss this Question
122. What is the use of PHP $_SERVER variable?
- To update the content on the server
- To access the information about headers, paths, and script locations
- To access and update the database records on the server
- All of the above
Answer: B) To access the information about headers, paths, and script locations
Explanation:
The PHP $_SERVER is used to get the information about headers, paths, and script locations.
Discuss this Question
123. Which element of the of PHP $_SERVER variable is used to get the path of the current script?
- SCRIPT_NAME
- SCRIPT_PATH
- PATH
- FILE_PATH
Answer: A) SCRIPT_NAME
Explanation:
The SCRIPT_NAME element is used to get the path of the current script.
Discuss this Question
124. Which element of the of PHP $_SERVER variable is used to get the filename of the currently executing script?
- PATH
- FILE_PATH
- PHP_SELF
- SELF
Answer: C) PHP_SELF
Explanation:
The PHP_SELF element is used to get the filename of the currently executing script.
Discuss this Question
125. Which PHP global variable is used to collect data after submitting an HTML form?
- $_GET
- $_REQUEST
- $_POST
- $_ENV
Answer: B) $_REQUEST
Explanation:
The PHP global variable $_REQUEST is used to collect data after submitting an HTML form.
Discuss this Question
126. Which PHP global variable is used to collect form data after submitting an HTML form with method="post"?
- $_GET
- $_REQUEST
- $_POST
- $_ENV
Answer: C) $_POST
Explanation:
The PHP global variable $_POST is used to collect form data after submitting an HTML form with method="post".
Discuss this Question
15) PHP Regular Expressions MCQs
127. What is the return value of preg_match() function?
- 0 or 1
- False or True
- 0, less than 1, or greater then 1
- None of the above
Answer: A) 0 or 1
Explanation:
The PHP function preg_match() return 1 if the given pattern was found in the string; 0, otherwise.
Discuss this Question
128. What will be the output of the following PHP code?
<?php
$str = "Hello, IncludeHelp";
$pattern = "/includehelp/i";
echo preg_match($pattern, $str);
?>
- 0
- 1
- 5
- 7
Answer: B) 1
Explanation:
In the above PHP script, the pattern is found in the string and the value i enables the case-insensitivity. Thus, the result will be 1.
Discuss this Question
129. What will be the output of the following PHP code?
<?php
$str = "Hello, IncludeHelp";
$pattern = "/el/m";
echo preg_match($pattern, $str);
?>
- 0
- 1
- 5
- 7
Answer: B) 1
Explanation:
In the above PHP script, the pattern is found in the string and the value "m" is used to perform a multiline search (it will search the patterns for the beginning or end of a string will match the beginning or end of each line). Thus, the result will be 1.
Discuss this Question
130. What are the metacharacters in PHP Regular Expression?
- Metacharacters are characters with a special meaning
- Metacharacters are set of symbols to perform the mathematical operations on the characters
- Metacharacters are characters written within the double quotes to search in the string
- None of the above
Answer: A) Metacharacters are characters with a special meaning
Explanation:
Metacharacters are characters with a special meaning.
Discuss this Question
131. What is the search pattern to find any character from the range 5 to 8?
- [5 to 8]
- [5...8]
- [5-8]
- [4-9]
Answer: C) [5-8]
Explanation:
The search pattern [5-8] is used to find any character from the range 5 to 8.
Discuss this Question
132. What is the valid set of characters (or, metacharacters) to find the Unicode character specified by the hexadecimal number xxxx?
- \uxxxx
- \xxxx
- \+uxxxx
- \+Uxxxx
Answer: A) \uxxxx
Explanation:
\uxxxx is used to find the Unicode character specified by the hexadecimal number xxxx.
Discuss this Question
16) PHP Date and Time MCQs
133. What is the complete syntax of PHP date() function?
- date(format)
- date(timestamp)
- date(format, timestamp)
- date(format, value, timestamp)
Answer: C) date(format, timestamp)
Explanation:
The complete syntax of PHP date() function is: date(format, timestamp).
Discuss this Question
134. Which is the correct PHP statement to print the current date in DD-MON-YYY format?
- <?php echo date("DD-MON-YYYY"); ?>
- <?php echo date("dd-mon-yyyy"); ?>
- <?php echo date("d-m-y"); ?>
- <?php echo date("d-M-Y"); ?>
Answer: D) <?php echo date("d-M-Y"); ?>
Explanation:
The correct PHP statement to print the current date in DD-MON-YYYY format is:
<?php echo date("d-M-Y"); ?>
Discuss this Question
135. What will be the output of the following PHP code (if the date is 30th December 2021)?
- 30th - Dec - 2021
- 30 (Thursday) - Dec - 2021
- 30 (Thu) - Dec - 2021
- 30 (l) - Dec - 2021
Answer: B) 30 (Thursday) - Dec - 2021
Explanation:
The output will be "30 (Thursday) - Dec - 2021". The meaning of the values used in the code,
- d – Displays the date
- l – Displays the weekday
- M – Display the month name in 3 characters
- Y – Displays the year in four digits
Discuss this Question
136. Which is the correct PHP statement to print the current time in the format of hour:minutes:seconds(am/pm) i.e., 10:22:54pm?
- <?php echo date("h:i:sa");?>
- <?php echo date("h:m:sa");?>
- <?php echo date("h:mi:sa");?>
- <?php echo date("hh:min:sa");?>
Answer: A) <?php echo date("h:i:sa");?>
Explanation:
The correct PHP statement to print the current time in the format of hour:minutes:seconds(am/pm) i.e., 10:22:54pm is:
<?php echo date("h:i:sa");?>
Discuss this Question
137. The PHP date() function returns the ____.
- Current date and time of the localhost
- Current date and time of the server
- Current UTC time
- All of the above
Answer: B) Current date and time of the server
Explanation:
The PHP date() function returns the current date and time of the server.
Discuss this Question
138. Which PHP function is used to add days, months, years, hours, minutes, and seconds to a date?
- add()
- add_date()
- date_add()
- add_dates()
Answer: C) date_add()
Explanation:
The PHP date_add() function is used to add days, months, years, hours, minutes, and seconds to a date.
Discuss this Question
139. Which PHP function modifies the timestamp?
- date_modify()
- modify_timestamp()
- date_stamp()
- stamp()
Answer: A) date_modify()
Explanation:
The PHP date_modify() function modifies the timestamp.
Discuss this Question
140. Which PHP function is used to get the current Unix timestamp with microseconds?
- date_microtime()
- microtime()
- mtime()
- microsecondstime()
Answer: B) microtime()
Explanation:
The PHP microtime() function is used to get the current Unix timestamp with microseconds.
Discuss this Question
141. Which PHP function is used to get the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)?
- time()
- gettime()
- stime()
- time_seconds()
Answer: A) time()
Explanation:
The PHP time() function is used to get the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Discuss this Question
142. What will be the output of the following PHP code (if the date is 30th December 2021)?
<?php
$d=mktime(12, 13, 14, 1, 2, 2021);
echo date("d-M-Y h:i:sa", $d);
?>
- 01-Feb-2021 12:13:14pm
- 01-feb-2021 14:13:12pm
- 02-Jan-2021 12:13:14pm
- 02-Jan-2021 14:13:12pm
Answer: C) 02-Jan-2021 12:13:14pm
Explanation:
The PHP mktime() function is used to get the Unix timestamp for a given date. The order of the parameters is,
mktime(hour, minute, second, month, day, year)
Discuss this Question
17) PHP include and require Statements MCQs
143. What is the difference of include and require statements in PHP?
- There is not difference both are the same
- require is an alias of include
- require returns the Fatal Error, while include returns the Warning
- include includes one PHP file while require can include multiple PHP files
Answer: C) require returns the Fatal Error, while include returns the Warning
Explanation:
The both include and require statements are used to insert the content of one file into another file. Since both are identical but require returns the Fatal Error, while include returns the Warning.
Discuss this Question
144. What is the syntax of PHP "include" statement?
- include {'filename'};
- <include 'filename'>;
- include <'filename'>;
- include 'filename';
Answer: D) include 'filename';
Explanation:
The syntax of the PHP include statement is:
include 'filename';
Discuss this Question
145. What is the syntax of PHP "require" statement?
- require {'filename'};
- <require 'filename'>;
- require <'filename'>;
- require 'filename';
Answer: D) require 'filename';
Explanation:
The syntax of the PHP “require” statement is:
require 'filename';
Discuss this Question
18) PHP File Handling MCQs
146. Which PHP function is used to open a file?
- fopen()
- open()
- open_file()
- PHP_open()
Answer: A) fopen()
Explanation:
The PHP fopen() function is used to open a file.
Discuss this Question
147. Which PHP function is used to close a file?
- fclose()
- close()
- close_file()
- PHP_close()
Answer: A) fclose()
Explanation:
The PHP fclose() function is used to close a file.
Discuss this Question
148. Which PHP function is used to read the content of a file?
- read()
- getfile()
- readfile()
- All of the above
Answer: C) readfile()
Explanation:
The PHP readfile() function is used to read the content of a file.
Discuss this Question
149. Which PHP function reads a single line from a file?
- readline()
- getline()
- fget()
- fgets()
Answer: D) fgets()
Explanation:
The PHP fgets() function reads a single line from a file.
Discuss this Question
150. Which PHP function reads a single character from a file?
- readcharacter()
- getchar()
- fgetc()
- fchars()
Answer: C) fgetc()
Explanation:
The PHP fgetc() function reads a single character from a file.
Discuss this Question