ADVERTISEMENT
ADVERTISEMENT

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

  1. PHP Basics MCQs
  2. PHP Variables, Variable Scopes MCQs
  3. PHP echo and print Statements MCQs
  4. PHP Data Types MCQs
  5. PHP Strings MCQs
  6. PHP Numbers MCQs
  7. PHP Math MCQs
  8. PHP Constants MCQs
  9. PHP Operators MCQs
  10. PHP Conditional Statements MCQs
  11. PHP Loops MCQs
  12. PHP Functions MCQs
  13. PHP Arrays MCQs
  14. PHP Superglobals MCQs
  15. PHP Regular Expressions MCQs
  16. PHP Date and Time MCQs
  17. PHP include and require Statements MCQs
  18. PHP File Handling MCQs

1) PHP Basics MCQs

1. PHP is an acronym for ____.

  1. Prefix Hypertext Preprocessor
  2. Prototype Hypertext Preprocessor
  3. Hypertext Preprocessor
  4. PHP: Hypertext Preprocessor

Answer: D) PHP: Hypertext Preprocessor

Explanation:

PHP is an acronym for "PHP: Hypertext Preprocessor".


2. Which is/are statement(s) true about PHP?

  1. It is an open-source scripting language
  2. PHP scripts execute on the server
  3. It is used for developing dynamic & interactive websites
  4. All of the above

Answer: D) All of the above

Explanation:

All of the above-mentioned statements are true about PHP.


3. What is the extension of a PHP file?

  1. .php
  2. .ph
  3. .phpfile
  4. All of the above

Answer: A) .php

Explanation:

The extension of a PHP file is .php.


4. Who developed PHP?

  1. Guido van Rossum
  2. Rasmus Lerdorf
  3. Jesse James Garrett
  4. Douglas Crockford

Answer: B) Rasmus Lerdorf

Explanation:

Rasmus Lerdorf developed PHP.


5. In which year PHP was developed?

  1. 1993
  2. 1994
  3. 1995
  4. 1996

Answer: B) 1994

Explanation:

PHP was developed in 1994.


ADVERTISEMENT

6. A PHP script starts with ____ and ends with ___.

  1. <?php and ?>
  2. <php> and </php>
  3. <?php and /?php>
  4. </php and />

Answer: A) <?php and ?>

Explanation:

A PHP script starts with <?php and ends with ?>.


7. PHP keywords are case-sensitive?

  1. Yes
  2. No

Answer: B) No

Explanation:

No, PHP keywords (example: if, else, while, echo, etc.) are not case-sensitive.


8. Single line comments can be placed in PHP script by using which symbol?

  1. //
  2. #
  3. $
  4. 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.


9. Multi-line comments can be written within the ____.

  1. // and //
  2. ## and ##
  3. /* and */
  4. /// and ///

Answer: C) /* and */

Explanation:

In PHP, the multi-line comments can be written within the /* and */.


10. PHP statements end with a ______.

  1. semicolon (;)
  2. colon (:)
  3. dot (.)
  4. comma (,)

Answer: A) semicolon (;)

Explanation:

PHP statements end with a semicolon (;).


ADVERTISEMENT

2) PHP Variables, Variable Scopes MCQs

11. Which sign is used to declare variables in PHP?

  1. @
  2. &
  3. $
  4. _

Answer: C) $

Explanation:

The dollar ($) sign is used to declare variables in PHP.


12. Which is not a valid variable name in PHP?

  1. age
  2. _age
  3. PersonAge
  4. 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.


13. Are variable names case-sensitive?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, variable names are case-sensitive. Example: variables $name, $Name, and $NAME are different.


14. Which statement is commonly used for PHP output?

  1. write
  2. php.write
  3. log
  4. echo

Answer: D) echo

Explanation:

The PHP echo statement is commonly used for PHP output.


15. How many variable scopes are there in PHP?

  1. 1
  2. 2
  3. 3
  4. 4

Answer: C) 3

Explanation:

There are 3 different variable scopes in PHP. Those are

  • local
  • global
  • static

16. Which is not a valid variable scope in PHP?

  1. local
  2. global
  3. static
  4. external

Answer: D) external

Explanation:

The external is not a valid variable scope in PHP.


17. A variable declared outside a function has a ____.

  1. local scope
  2. global scope

Answer: B) global scope

Explanation:

A variable declared outside a function has a global scope.


18. What will be the output of the following PHP code?

<?php
$x = 5;

function myFunction(){
    echo "Result $x";
}
myFunction();
?>
  1. Result $x
  2. Result 5
  3. Result
  4. None of the above

Answer: C) Result

Explanation:

The variable x is a global variable, and can only be accessed outside a function.


19. What will be the output of the following PHP code?

<?php
function myFunction()
{
    $x = 5;
    echo "Result1: $x , ";
}
myFunction();
echo "Result2: $x";
?>
  1. Result1: 5 , Result2:
  2. Result1: 5 , Result2: 0
  3. Result1: 5 , Result2: 5
  4. 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.


20. Which PHP keyword is used to access a global variable inside the function?

  1. php_ global
  2. global
  3. global_variable
  4. globalscope

Answer: B) global

Explanation:

PHP keyword global is used to access a global variable inside the function.


21. There are two variables a, b which declared in global scope, which is the correct PHP statement to access them within a function?

  1. global $a, $b;
  2. global $a $b;
  3. global ($a, $b);
  4. php_global $a, $b;

Answer: A) global $a, $b;

Explanation:

The correct PHP statement to access them within a function is: global $a, $b;


22. What is the name of an array that stores all global variables in PHP?

  1. $GLOBAL[]
  2. $global[]
  3. $GLOBALS[]
  4. $PHP_GLOBALS[]

Answer: C) $GLOBALS[]

Explanation:

PHP has an array named $GLOBALS[] that stores all global variables in PHP.


23. In the syntax of $GLOBALS[index], what does "index" hold?

  1. Index (starting from 0) of the variable
  2. Index (starting from 1) of the variable
  3. Name of the variable
  4. 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.


24. What will be the output of the following PHP code?

<?php
function Increment(){
    static $num = 0;
    echo "$num";
    $num++;
}

Increment();
Increment();
Increment();
?>
  1. 000
  2. 111
  3. 011
  4. 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".

3) PHP echo and print Statements MCQs

25. Which statement is faster echo or print?

  1. echo
  2. print

Answer: A) echo

Explanation:

The echo statement is marginally faster than print statement.


26. What is the correct syntax of echo statement in PHP?

  1. echo
  2. echo()
  3. echo = ()
  4. Both A. and B.

Answer: D) Both A. and B.

Explanation:

The echo statement can be used with or without parentheses.


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".

  1. echo "Hello $name";
  2. echo "Hello " . $name;
  3. echo ("Hello $name");
  4. 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".


28. What is the correct syntax of print statement in PHP?

  1. print
  2. print()
  3. print = ()
  4. Both A. and B.

Answer: D) Both A. and B.

Explanation:

The print statement can be used with or without parentheses.


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".

  1. print "Hello $name";
  2. print "Hello " . $name;
  3. print ("Hello $name");
  4. 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".


30. What will be the output of the following PHP code?

<?php
$a = 5;
$b = 10;

print ("$a, $b+10");
?>
  1. 5, 10+10
  2. 5, 20
  3. 5 10+10
  4. 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.


31. What will be the output of the following PHP code?

<?php
var_dump (print "Hello");
?>
  1. Helloint(5)
  2. Helloint(6)
  3. Helloint(1)
  4. 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)".

4) PHP Data Types MCQs

32. Which data type is not supported by PHP?

  1. Integer
  2. Complex
  3. Float
  4. String

Answer: B) Complex

Explanation:

PHP does not support "Complex" data type.


33. String is placed within _____.

  1. Double quotes ("")
  2. Single quotes ('')
  3. Both A. and B.
  4. None of the above

Answer: C) Both A. and B.

Explanation:

String is written within either single quotes ('') or double quotes ("").


34. A PHP integer data type can store values between ____.

  1. -65,536 and 65,535
  2. 0 and 4,294,967,295
  3. -(2^63) and (2^63)-1
  4. -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.


35. "Array" is a data type in PHP?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, Array is a data type in PHP.


36. What will be the output of the following PHP code?

<?php  
$x = true;
var_dump($x);
?>  
  1. boolean('true')
  2. boolean(true)
  3. bool(true)
  4. 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).


37. What will be the output of the following PHP code?

<?php
$Laptops = array(
    "MAC",
    "Lenovo",
    "Dell",
    "HP"
);
echo gettype($Laptops);
?>
  1. array
  2. array(4)
  3. object
  4. 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.


38. What is NULL in PHP?

  1. Value
  2. Keyword
  3. Data Type
  4. Function

Answer: C) Data Type

Explanation:

PHP introduced a special data type that is NULL which can have only one value: null.


39. What will be the output of the following PHP code?

<?php
$value1 = NULL;
$value2 = null;

var_dump($value1);
var_dump($value2);
?>  
  1. Error
  2. NULL undefined
  3. NULL object
  4. NULL NULL

Answer: D) NULL NULL

Explanation:

Both of the values "NULL" and "null" are NULL. Thus, the output will be "NULL NULL".


40. Which type is used to store the database call?

  1. object
  2. class
  3. resource
  4. 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.

5) PHP Strings MCQs

41. What will be the output of the following PHP code?

<?php
echo strpos("Hello, Includehelp!", "Includehelp!");
?>  
  1. 6
  2. 7
  3. 8
  4. -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.


42. Which function is used to replace text within a string?

  1. str_replace()
  2. replace()
  3. replace_str()
  4. string-replace()

Answer: A) str_replace()

Explanation:

The str_replace() is used to replace text within a string.


43. What will be the output of the following PHP code?

<?php 
$str = addcslashes("IncludeHelp","e");
echo($str); 
?>
  1. Includehelp
  2. IncludeH\elp
  3. Includ\eHelp
  4. Includ\eH\elp

Answer: D) Includ\eH\elp

Explanation:

The addcslashes() function returns a string with backslashes in front of the specified characters.


44. What will be the output of the following PHP code?

<?php 
$str = addslashes('Hello "Includehelp!"');
echo($str); 
?>
  1. Hello Includehelp!
  2. Hello "Includehelp!"
  3. Hello ""Includehelp!""
  4. Hello \"Includehelp!\"

Answer: D) Hello \"Includehelp!\"

Explanation:

The addslashes() function is used to get a string with backslashes in front of predefined characters.


45. Which function converts a string of ASCII characters to hexadecimal values?

  1. bin2hex()
  2. str2hex()
  3. hex()
  4. str_hex()

Answer: A) bin2hex()

Explanation:

The bin2hex() function converts a string of ASCII characters to hexadecimal values.


46. What will be the output of the following PHP code?

<?php
$str = "Includehelp";
echo pack("H*",bin2hex($str));
?> 
  1. 496e636c75646568656c70
  2. Includehelp
  3. 496e636c75646568656c70Includehelp
  4. 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.


47. Which function is used to convert the ASCII value to the character?

  1. asc()
  2. str()
  3. char()
  4. chr()

Answer: D) chr()

Explanation:

The chr() function is used to convert the given ASCII value to the character.


48. What is the complete syntax of chunk_split() function in PHP?

  1. chunk_split(string,length,end)
  2. chunk_split(string)
  3. chunk_split(string,end)
  4. chunk_split(length)

Answer: A) chunk_split(string,length,end)

Explanation:

The complete syntax of chunk_split() function is:

chunk_split(string,length,end)

49. Which function is used to convert hexadecimal values to the ASCII characters?

  1. hex2bin()
  2. hex2str()
  3. hex2asc()
  4. hex_string()

Answer: A) hex2bin()

Explanation:

The hex2bin() function is used to convert hexadecimal values to the ASCII characters.


50. What is the complete syntax of join() function in PHP?

  1. join(string1, string2)
  2. join(string1, string2, separator)
  3. join(separator, array)
  4. join(separator, string1, string2)

Answer: C) join(separator, array)

Explanation:

The complete syntax of join() function is:

join(separator, array)

51. There are two strings (in an array) "Hello" and "World", which is the correct PHP statement to join them with the separator ","?

  1. join(",","Hello","World")
  2. join("Hello","World",",")
  3. join(",", ("Hello","World"))
  4. 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"))

52. Which PHP function is used to print (output) a formatted string?

  1. echo()
  2. print()
  3. printf()
  4. prints()

Answer: C) printf()

Explanation:

The printf() function is used to print (output) a formatted string.


53. What will be the output of the following PHP code?

<?php
printf("%x",65535);
?>  
  1. ffff
  2. 0xffff
  3. FFFF
  4. 0XFFFF

Answer: A) ffff

Explanation:

%x in printf() function prints/converts the number in hexadecimal number where letters are in lowercase.


54. Which PHP function is used to get the length of the string?

  1. strlength()
  2. strlen()
  3. length()
  4. str_len()

Answer: C) length()

Explanation:

The length() function is used to get the length of the string.


55. What will be the output of the following PHP code?

<?php
echo ucwords("hi, there how are you?");
?>  
  1. Hi, there how are you?
  2. Hi, There How Are You?
  3. hi, there how are you?
  4. 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?".


56. Which PHP functions are used to convert string to lowercase and uppercase?

  1. strupper() and strlower()
  2. str_toupper() and str_tolower()
  3. toupper() and tolower()
  4. 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.

6) PHP Numbers MCQs

57. Which PHP constant returns the largest integer supported?

  1. INT_MAX
  2. MAX_INT
  3. MAXINT
  4. PHP_INT_MAX

Answer: D) PHP_INT_MAX

Explanation:

The PHP_INT_MAX is a PHP predefined constant which returns the largest integer supported.


58. Which PHP constant returns the size of an integer in bytes?

  1. INT_SIZE
  2. SIZE_INT
  3. INTSIZE
  4. 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.


59. Which is/are the function(s) to check if the type of a variable is integer?

  1. is_int()
  2. is_integer()
  3. is_long()
  4. 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.


60. What will be the output of the following PHP code?

<?php
$x = -123.45;
echo var_dump(is_int($x));
?>
  1. bool(false)
  2. bool(true)
  3. false
  4. 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)".


61. Which is/are the function(s) to check if the type of a variable is float?

  1. is_float()
  2. is_double()
  3. is_longfloat()
  4. 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.


62. Which is/are the function(s) to check if a numeric value is finite or infinite?

  1. is_finite()
  2. is_infinite()
  3. is_inf()
  4. 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.


63. PHP NaN stands for ____.

  1. not-a-number
  2. not-a-numerical
  3. nothing-a-number
  4. numeric-a-number

Answer: A) not-a-number

Explanation:

PHP NaN stands for "not-a-number".


64. What will be the output of the following PHP code?

<?php
echo var_dump(is_nan(10));
?>
  1. bool(false)
  2. bool(true)
  3. false
  4. 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)".


65. Which is the correct syntax to cast float to integer?

  1. (int) float_variable
  2. int (float_variable)
  3. (int) (float_variable)
  4. All of the above

Answer: A) (int) float_variable

Explanation:

The correct syntax to cast float to integer is:

(int) float_variable

7) PHP Math MCQs

66. Which PHP function is used to get the value of PI?

  1. php_pi()
  2. pivalue()
  3. pi()
  4. None of the above

Answer: C) pi()

Explanation:

The pi() function is used to get the value of PI.


67. Which PHP functions are used to find the lowest and highest values from a list of arguments?

  1. minimum() and maximum()
  2. min() and max()
  3. find_min() and find_max()
  4. 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.


68. Which PHP function is used to get the absolute value of the given number?

  1. abs()
  2. absolute()
  3. find_abs()
  4. php_abs()

Answer: A) abs()

Explanation:

The PHP function abs() is used to get the absolute value of the given number.


69. Which PHP function is used to get the square root of the given number?

  1. sqrt()
  2. squareroot()
  3. find_sqrt()
  4. php_sqrt()

Answer: A) sqrt()

Explanation:

The PHP function abs() is used to get the square root of the given number.


70. What will be the output of the following PHP code?

<?php
echo (round(0.49) . "," . round(0.50) . "," . round(0.51));
?>
  1. 0,0,0
  2. 1,1,1
  3. 0,0,1
  4. 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.


71. Which PHP function is used to get the random number?

  1. random()
  2. randomize()
  3. rand()
  4. randnumbers()

Answer: C) rand()

Explanation:

The PHP function rand() is used to get the random number.


72. Which PHP function is used to convert a number from one number base to another?

  1. convert()
  2. base_convert()
  3. parseInt()
  4. base_conversion()

Answer: B) base_convert()

Explanation:

The PHP function base_convert() is used to convert a number from one number base to another.


73. What is the correct syntax of PHP base_convert() function?

  1. base_convert(number, from_base, to_base);
  2. base_convert(number, to_base, from_base);
  3. base_convert(number);
  4. 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);

74. What is the use of PHP mt_rand() function?

  1. Generates a random number using the milliseconds time.
  2. Generates a random number using the microseconds time.
  3. Generates a random number using the milliseconds time.
  4. 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.


75. What is the use of PHP lcg_value() function?

  1. Generates a pseudo random number in a range between 0 and 1
  2. Generates a pseudo random number in a range between the given minimum to maximum numbers
  3. Generates a pseudo random number in a range between 0 and 1 using the Mersenne Twister algorithm
  4. 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.

8) PHP Constants MCQs

76. Which keyword/function is used to create a constant in PHP?

  1. define keyword
  2. define() function
  3. const keyword
  4. const() function

Answer: B) define() function

Explanation:

The define() function is used to create a constant in PHP.


77. Which is the correct syntax of define() function in PHP?

  1. define(constant_name)
  2. define(constant_name, value)
  3. define(constant_name, value, case-insensitive)
  4. 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)

78. What can be the value of case-insensitive parameter in define() function?

  1. true
  2. false
  3. Anything
  4. Both A. and B.

Answer: D) Both A. and B.

Explanation:

The case-insensitive parameter may have either true or false.


79. Give an example, how to define a constant array in PHP?

  1. const cities = new array(["New Delhi","Mumbai","Banglore"]);
  2. define("cities"=["New Delhi","Mumbai","Banglore"]);
  3. define("cities", ["New Delhi","Mumbai","Banglore"]);
  4. 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"]);

80. What is the scope of a constant in PHP?

  1. Local
  2. Global
  3. Static
  4. Fixed

Answer: B) Global

Explanation:

The constants are automatically global and can be used across the entire script.

9) PHP Operators MCQs

81. What is name of PHP "===" operator?

  1. Equal
  2. Safe Equal
  3. Identity
  4. 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.


82. What will be the output of the following PHP code?

<?php
$a = 123;
$b = "123";

var_dump($a !== $b); 
?>
  1. Error
  2. bool(false)
  3. bool(true)
  4. 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).


83. What is name of PHP "<=>" operator?

  1. Spaceship
  2. Safe Spaceship
  3. Identity
  4. 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.


84. Does PHP support Ternary Operator?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, PHP supports Ternary Operator. The operator ?: is used as a Ternary Operator.


85. Which PHP operator known as "Null coalescing" operator?

  1. ?
  2. ?=>
  3. ??
  4. ???

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.

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";
}
?>
  1. True
  2. False

Answer: A) True

Explanation:

In the above PHP script, the expression is true. Thus, the output will be "True".


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";
}
?>
  1. True
  2. False
  3. TrueFalse
  4. 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.


88. Which PHP statement is used to select one of many blocks of code to be executed?

  1. The if statement
  2. The if...else statement
  3. The switch statement
  4. 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.


89. In PHP, the break statements with case blocks are optional?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, the break statements with case blocks are optional.


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.";
}
?>
  1. You selected Honda.
  2. You selected Honda.You selected BMW.
  3. You selected Honda.You selected BMW.You selected Audi.
  4. 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.

11) PHP Loops MCQs

91. How many loops are there in PHP?

  1. 2
  2. 3
  3. 4
  4. 5

Answer: C) 4

Explanation:

There are 4 types of Loops are there in PHP which are while, do...while, for, and foreach.


92. What will be the output of the following PHP code?

<?php
$counter = 1;

while ($counter++ <= 5)
{
    echo $counter, ",";
    $counter++;
}
?>  
  1. 2,4,
  2. 1,2,3,4,5,
  3. 2,4,6,
  4. 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,".


93. Which loop statement is used to loop through a block of code a specified number of times?

  1. while
  2. do...while
  3. for
  4. 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.


94. Which loop statement is used to loop through a block of code for each element in an array?

  1. while
  2. do...while
  3. for
  4. 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.


95. What will be the output of the following PHP code?

<?php
$cars = array(
    "BMW",
    "Mercedes",
    "Honda",
);

foreach ($cars as $c)
{
    echo "$c ";
}
?>
  1. BMW Mercedes Honda
  2. Honda Mercedes BMW
  3. SyntaxError
  4. 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".


96. Which PHP statement is used to jump out of a loop?

  1. exit
  2. break
  3. continue
  4. stop

Answer: B) break

Explanation:

In PHP, the break statement is used to jump out of a loop.


97. What is the use of PHP "continue" statement?

  1. breaks the loop and transfers the control to the statement written just after the loop body
  2. breaks the all loop statement (outer loops and inner loops)
  3. breaks one iteration of the loop and transfers the control to the next loop iteration
  4. 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.


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. 1 2 3 4 5 7 8 9 10
  2. 1 2 3 4 5 6 7 8 9 10
  3. 1 2 3 4 5 6
  4. 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".


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?

  1. Yes
  2. No

Answer: B) No

Explanation:

No, we cannot use the continue statement instead of the break statement in the switch statement.


100. What will be the output of the following PHP code?

<?php  
$x = 10; 
  
while ($x < 10) { 
    $x++;
    echo $x, ","; 
} 
?>
  1. Infinite loop
  2. Error
  3. 10, 11,
  4. 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.

12) PHP Functions MCQs

101. Which keyword is used to define a function in PHP?

  1. def
  2. fun
  3. func
  4. function

Answer: D) function

Explanation:

The function keyword is used to define a function in PHP.


102. The PHP function names are case-sensitive?

  1. Yes
  2. No

Answer: B) No

Explanation:

No, the PHP function names are not case-sensitive.


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");
?>
  1. Name: Alvin, Age: 21
  2. Name: Alvin, Age:
  3. Name: Alvin, Age: 0
  4. 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

104. Which is the correct syntax of defining a default argument value in PHP?

  1. function function_name(type $argument_name : value) { /* function body*/ }
  2. function function_name(type $argument_name , value) { /* function body*/ }
  3. function function_name(type $argument_name = value) { /* function body*/ }
  4. 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*/ 
}

105. Which is the correct syntax of defining function return type in PHP?

  1. function function_name(type $arguments) : return_type { /* function body*/ }
  2. function function_name(type $arguments) , return_type { /* function body*/ }
  3. return_type function function_name(type $arguments) { /* function body*/ }
  4. 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*/ 
}

13) PHP Arrays MCQs

106. Which PHP function is used to create an array?

  1. array()
  2. arr()
  3. new_array()
  4. array()

Answer: A) array()

Explanation:

The PHP array() function is used to create an array.


107. How many types of arrays are there in PHP?

  1. 1
  2. 2
  3. 3
  4. 4

Answer: C) 3

Explanation:

There are 3 types of arrays are there in PHP.


108. Which is/are valid types of arrays in PHP?

  1. Indexed arrays
  2. Associative arrays
  3. Multidimensional arrays
  4. All of the above

Answer: D) All of the above

Explanation:

All of the above types are valid arrays in PHP.


109. What is the difference between Indexed array and Associative array in PHP?

  1. Index array has numeric index while associative array has named keys
  2. Index array has numeric index while associative array has named keys and numeric index both
  3. Index array is one-dimensional array with numeric index while associative array is two-dimensional array with numeric index
  4. 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.


110. Which is the correct example of an Indexed array in PHP?

  1. $cities = array("Delhi"; "Mumbai"; "Banglore");
  2. $cities = array("Delhi", "Mumbai", "Banglore");
  3. $cities = new array("Delhi", "Mumbai", "Banglore");
  4. $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");

111. Which is the correct example of an Associative array in PHP?

  1. $person = array("Alvin"=>"Delhi", "Alex"=>"Mumbai", "Bhavik"=>"Banglore");
  2. $person = array("Alvin"=>"Delhi"; "Alex"=>"Mumbai"; "Bhavik"=>"Banglore");
  3. $person = new array("Alvin"=>"Delhi", "Alex"=>"Mumbai", "Bhavik"=>"Banglore");
  4. $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");

112. Which PHP function(s) is/are used to compare arrays and returns the differences?

  1. array_diff()
  2. array_diff_assoc()
  3. array_diff_key()
  4. 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.


113. What is the difference between array_diff() and array_diff_assoc() functions?

  1. array_diff() compares the values only while array_diff_assoc() compares the keys only
  2. array_diff() compares the values only while array_diff_assoc() compares the keys and values
  3. Both functions can be used to compare the values and keys
  4. 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.


114. Which PHP function is used to sort multi-dimensional arrays?

  1. array_sort()
  2. sort()
  3. multisort()
  4. array_multisort()

Answer: D) array_multisort()

Explanation:

The PHP function array_multisort() is used to sort multi-dimensional arrays.


115. What is the use of PHP sort() function?

  1. Sorts an indexed array
  2. Sorts an associative array
  3. Sorts a multi-dimensional array
  4. Sorts any kind of array

Answer: A) Sorts an indexed array

Explanation:

The PHP function sort() is used to sort an indexed array.


116. Which PHP function is used to sort an indexed array in descending order?

  1. sort_reverse()
  2. reverse_sort()
  3. revsort()
  4. rsort()

Answer: D) rsort()

Explanation:

The PHP function rsort() is used to sort an indexed array in descending order.


117. Which PHP function is used to return the current element in an array?

  1. get()
  2. start()
  3. current()
  4. cur()

Answer: C) current()

Explanation:

The PHP function current() is used to return the current element in an array.


118. Which PHP function is used to check if a specified value exists in an array?

  1. in_array()
  2. check_array()
  3. exist()
  4. 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.


119. Which PHP function is used to sort an associative array in descending order, according to the key?

  1. sort()
  2. rsort()
  3. ksort()
  4. krsort()

Answer: D) krsort()

Explanation:

The PHP function krsort() is used to sort an associative array in descending order, according to the key.


120. Which PHP function is used to get one or more random keys from an array?

  1. array_random()
  2. array_randomize()
  3. array_rand()
  4. krand()

Answer: C) array_rand()

Explanation:

The PHP function array_rand() is used to get one or more random keys from an array.

14) PHP Superglobals MCQs

121. Which is not a valid PHP superglobal variable?

  1. $GLOBALS
  2. $_SERVER
  3. $_ENV
  4. $_SESSIONS

Answer: D) $_SESSIONS

Explanation:

$_SESSIONS is not a PHP Superglobal variable.


122. What is the use of PHP $_SERVER variable?

  1. To update the content on the server
  2. To access the information about headers, paths, and script locations
  3. To access and update the database records on the server
  4. 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.


123. Which element of the of PHP $_SERVER variable is used to get the path of the current script?

  1. SCRIPT_NAME
  2. SCRIPT_PATH
  3. PATH
  4. FILE_PATH

Answer: A) SCRIPT_NAME

Explanation:

The SCRIPT_NAME element is used to get the path of the current script.


124. Which element of the of PHP $_SERVER variable is used to get the filename of the currently executing script?

  1. PATH
  2. FILE_PATH
  3. PHP_SELF
  4. SELF

Answer: C) PHP_SELF

Explanation:

The PHP_SELF element is used to get the filename of the currently executing script.


125. Which PHP global variable is used to collect data after submitting an HTML form?

  1. $_GET
  2. $_REQUEST
  3. $_POST
  4. $_ENV

Answer: B) $_REQUEST

Explanation:

The PHP global variable $_REQUEST is used to collect data after submitting an HTML form.


126. Which PHP global variable is used to collect form data after submitting an HTML form with method="post"?

  1. $_GET
  2. $_REQUEST
  3. $_POST
  4. $_ENV

Answer: C) $_POST

Explanation:

The PHP global variable $_POST is used to collect form data after submitting an HTML form with method="post".

15) PHP Regular Expressions MCQs

127. What is the return value of preg_match() function?

  1. 0 or 1
  2. False or True
  3. 0, less than 1, or greater then 1
  4. 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.


128. What will be the output of the following PHP code?

<?php
$str = "Hello, IncludeHelp";
$pattern = "/includehelp/i";

echo preg_match($pattern, $str); 
?>
  1. 0
  2. 1
  3. 5
  4. 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.


129. What will be the output of the following PHP code?

<?php
$str = "Hello, IncludeHelp";
$pattern = "/el/m";

echo preg_match($pattern, $str); 
?>
  1. 0
  2. 1
  3. 5
  4. 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.


130. What are the metacharacters in PHP Regular Expression?

  1. Metacharacters are characters with a special meaning
  2. Metacharacters are set of symbols to perform the mathematical operations on the characters
  3. Metacharacters are characters written within the double quotes to search in the string
  4. None of the above

Answer: A) Metacharacters are characters with a special meaning

Explanation:

Metacharacters are characters with a special meaning.


131. What is the search pattern to find any character from the range 5 to 8?

  1. [5 to 8]
  2. [5...8]
  3. [5-8]
  4. [4-9]

Answer: C) [5-8]

Explanation:

The search pattern [5-8] is used to find any character from the range 5 to 8.


132. What is the valid set of characters (or, metacharacters) to find the Unicode character specified by the hexadecimal number xxxx?

  1. \uxxxx
  2. \xxxx
  3. \+uxxxx
  4. \+Uxxxx

Answer: A) \uxxxx

Explanation:

\uxxxx is used to find the Unicode character specified by the hexadecimal number xxxx.

16) PHP Date and Time MCQs

133. What is the complete syntax of PHP date() function?

  1. date(format)
  2. date(timestamp)
  3. date(format, timestamp)
  4. date(format, value, timestamp)

Answer: C) date(format, timestamp)

Explanation:

The complete syntax of PHP date() function is: date(format, timestamp).


134. Which is the correct PHP statement to print the current date in DD-MON-YYY format?

  1. <?php echo date("DD-MON-YYYY"); ?>
  2. <?php echo date("dd-mon-yyyy"); ?>
  3. <?php echo date("d-m-y"); ?>
  4. <?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"); ?>

135. What will be the output of the following PHP code (if the date is 30th December 2021)?

  1. 30th - Dec - 2021
  2. 30 (Thursday) - Dec - 2021
  3. 30 (Thu) - Dec - 2021
  4. 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

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?

  1. <?php echo date("h:i:sa");?>
  2. <?php echo date("h:m:sa");?>
  3. <?php echo date("h:mi:sa");?>
  4. <?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");?>

137. The PHP date() function returns the ____.

  1. Current date and time of the localhost
  2. Current date and time of the server
  3. Current UTC time
  4. 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.


138. Which PHP function is used to add days, months, years, hours, minutes, and seconds to a date?

  1. add()
  2. add_date()
  3. date_add()
  4. 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.


139. Which PHP function modifies the timestamp?

  1. date_modify()
  2. modify_timestamp()
  3. date_stamp()
  4. stamp()

Answer: A) date_modify()

Explanation:

The PHP date_modify() function modifies the timestamp.


140. Which PHP function is used to get the current Unix timestamp with microseconds?

  1. date_microtime()
  2. microtime()
  3. mtime()
  4. microsecondstime()

Answer: B) microtime()

Explanation:

The PHP microtime() function is used to get the current Unix timestamp with microseconds.


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)?

  1. time()
  2. gettime()
  3. stime()
  4. 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).


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);
?>
  1. 01-Feb-2021 12:13:14pm
  2. 01-feb-2021 14:13:12pm
  3. 02-Jan-2021 12:13:14pm
  4. 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)

17) PHP include and require Statements MCQs

143. What is the difference of include and require statements in PHP?

  1. There is not difference both are the same
  2. require is an alias of include
  3. require returns the Fatal Error, while include returns the Warning
  4. 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.


144. What is the syntax of PHP "include" statement?

  1. include {'filename'};
  2. <include 'filename'>;
  3. include <'filename'>;
  4. include 'filename';

Answer: D) include 'filename';

Explanation:

The syntax of the PHP include statement is:

include 'filename';

145. What is the syntax of PHP "require" statement?

  1. require {'filename'};
  2. <require 'filename'>;
  3. require <'filename'>;
  4. require 'filename';

Answer: D) require 'filename';

Explanation:

The syntax of the PHP “require” statement is:

require 'filename';

18) PHP File Handling MCQs

146. Which PHP function is used to open a file?

  1. fopen()
  2. open()
  3. open_file()
  4. PHP_open()

Answer: A) fopen()

Explanation:

The PHP fopen() function is used to open a file.


147. Which PHP function is used to close a file?

  1. fclose()
  2. close()
  3. close_file()
  4. PHP_close()

Answer: A) fclose()

Explanation:

The PHP fclose() function is used to close a file.


148. Which PHP function is used to read the content of a file?

  1. read()
  2. getfile()
  3. readfile()
  4. All of the above

Answer: C) readfile()

Explanation:

The PHP readfile() function is used to read the content of a file.


149. Which PHP function reads a single line from a file?

  1. readline()
  2. getline()
  3. fget()
  4. fgets()

Answer: D) fgets()

Explanation:

The PHP fgets() function reads a single line from a file.


150. Which PHP function reads a single character from a file?

  1. readcharacter()
  2. getchar()
  3. fgetc()
  4. fchars()

Answer: C) fgetc()

Explanation:

The PHP fgetc() function reads a single character from a file.

ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.