Top 100 PHP Interview Questions and Answers

PHP can be defined as a general-purpose scripting language that is mainly suitable for web development. It was created by Danish-Canadian programmer Rasmus Lerdorf in 1994. The PHP reference implementation is produced by PHP Group.

Contents show

Why do we use PHP?

PHP can be defined as a server-side scripting language that is mainly used to develop Dynamic websites or Static websites, or Web applications. PHP means Hypertext Pre-processor, which earlier stood for Personal Home Pages. PHP scripts will be interpreted only on a server that has PHP installed.

If you are planning to attend a PHP Interview or If you have a PHP Interview Scheduled nearby, then it’s a good idea to go through some of the PHP Interview questions and answers before you attend the interview. We have listed a few PHP Interview questions; make sure you go through all of them.

Top PHP Interview Questions and Answers

1. Define PHP in simple terms?

PHP (Hypertext Preprocessor ) can be defined as an open-source general-purpose scripting language that is specially designed for web development, and we can embed it into HTML.

2. To which programming language does PHP resemble?

PHP syntax usually resembles Perl and C.

3. List some of the common uses of PHP?

  1. It can encrypt data.
  2. It can perform system functions, i.e., from files on a system, it can create, read, open, write, and even close them.
  3. Using PHP, one can restrict the user from accessing some pages of the website.
  4. PHP can handle forms, i.e., it can gather data from files, save the data to a file, through an email one can send data, or return data to the user.
  5. It can access cookies variables and set cookies also.
  6. You can add, delete, modify elements within the database via PHP.

4. Define PEAR in PHP?

PEAR (PHP Extension and Application Repository). It can be defined as a framework and distribution system for the reusable PHP components. It extends the PHP and gives a high level of programming for web developers. PEAR is mainly  divided into three different classes:

  1. PEAR Core Components
  2. PEAR Packages
  3. PECL Packages

PHP Interview Questions and Answers

5. Can you name the recommended PHP version?

Version 7.1 or 7.2 is usually the recommended version of PHP.

6. Differentiate between static and dynamic websites?

Static WebsiteDynamic Website
These website pages with content cannot be changed without a developer editing its source code.In these types of websites, the content of the script can be changed at run time. The content is regenerated every time the user visits or reloads the website. 
Here, the theme and content of web pages will remain fixed.Here, the theme gets changed according to run time.
Here, changing the content is a tedious task as you need to develop and upload a new page.In the case of dynamic web pages, server applications do this automatically.

7. Can you tell us the old name of PHP?

The old name of PHP was Personal Home Page.

8. How can one execute a PHP script from the command line?

You can use the PHP command-line interface, i.e., CLI, and specify the file name of the script to be executed.

php script.php

9. What does ‘escaping to PHP mean’?

We use the Escape sequences for escaping a character during the string parsing. It is used to give a special meaning to define line breaks, tabs, alert and more. The escape sequences are usually interpolated into strings that are enclosed by double quotations or heredoc syntax.

PHP Interview Questions and Answers

10. Can you name the scripting engine in PHP?

The scripting engine that powers PHP is known as Zend Engine 2.

11. How can you run the interactive PHP shell from the command line interface?

We use the PHP CLI program with the option -a as:

php -a

12. List the characteristics of PHP variables?


  1. A variable starts with a $ sign, then, followed by the name of a variable.
  2. A variable name should consist of alphanumeric characters and underscores, i.e., A-z, 0-9, and _.
  3. A variable name has to start with a letter or the underscore character.
  4. Here, the variable names are basically case-sensitive ($Person and $PersSON are two different variables).
  5. One can’t start a variable name with a number.

13. Differentiate between PHP4 and PHP5?

All class variables are public.The class variables can have differing access levels.
Autoloading does not exist.Autoloading exists when you write an __autoload function.
Constructors are named after their class.Constructors are named __construct.
No concept of destructor.Destructors are named __destruct.

14. Can you name the two common ways to start and finish a PHP block of code?

The two common ways to start and finish a PHP script are given below:

<?php [   —  PHP code—- ] ?>

<? [—  PHP code  —] ?>

15. Name the different types of PHP variables?

PHP Interview Questions - different types of PHP variables
  1. Doubles − they are the floating-point numbers, like 9.14159 or 89.1.
  2. Integers − They are whole numbers, like 8195.
  3. NULL – It is a special type that has only one value called NULL.
  4. Booleans − They have only two possible values, i.e., either true or false.
  5. Strings − They are a sequence of characters, such as  ‘PHP Interview.’
  6. Objects − They are instances of programmer-defined classes that can package up both kinds of values and functions which are specific to a class.
  7. Arrays − They are the named and indexed collections of other values.
  8. Resources − They are special variables that can hold references to the resources external to PHP.

PHP Interview Questions and Answers

16. Can you name the popular Content Management Systems (CMS) in PHP?

  1. WordPress
  2. Joomla
  3. Magento
  4. Drupal

17. How to display the output directly to the browser?

To display output directly to the browser, we need to use the special tags <?= and ?>.

18. List the rules for naming a PHP variable?

  1. A variable name has to start with a letter or underscore character.
  2.  One can’t start a variable name with a number. 
  3. A variable name should contain only alpha-numeric characters and underscores such as (A-z, 0-9, and _ ).

19. Name the popular frameworks in PHP?

  1. CakePHP
  2. CodeIgniter
  3. Yii 2
  4. Symfony
  5. Zend Framework etc

20. Can PHP support multiple inheritances?

PHP will support only single inheritance, which means that a class can be extended from one single class only by using the keyword ‘extended.

PHP Interview Questions and Answers

21. Name some of the features of PHP7?

  1. Anonymous classes
  2. Closure::call method
  3. Group use declaration
  4. Scalar type declarations
  5. Return type declarations
  6. Null coalescing operator (??)
  7. Spaceship operator
  8. Constant arrays using define()
  9. Generator return expressions
  10. Generator delegation
  11. Space ship operator

22. Define a final class and a final method?

The final keyword in PHP prevents the child classes from overriding a method by prefixing a definition with final. It means that when we are using a method with the final keyword, it prevents us from overriding the method.

When we declare any method as final, then it means that we can perform overriding on that method. Methods are declared as final due to design reasons. The method should not be overridden due to some security or any other reasons.

23. Can you list the rules to determine the “truth” of any value which is not already of a Boolean type?

  1. If a value is a number, then it is false if exactly equal to zero or true otherwise.
  2. If a value is a string, ten, it is false if the string is empty (i.e., has zero characters) or is the string “0”, and it is true otherwise.
  3. The values of type NULL are always false.
  4. If a value is an array, then it is false if it consists of no other values, and it is otherwise true. An object that contains a value defines having a member variable has assigned a value.
  5. Valid resources are true.
  6. Don’t use double as Booleans.

24. Define “echo” in PHP?

The PHP echo statement is used to print a string, multi-line strings, variable, escaping characters, array, etc. echo is defined as a statement used to display the output. Echo is used with or without parentheses: echo(), and echo. Echo will not return any value.

25. How to compare objects in PHP?

In PHP, by using the comparison operator ( == ), the object variables are compared in a simple way, namely: The two object instances are considered equal if they have the same values and attributes (values are compared with == ), and are the instances of the same class.

PHP Interview Questions and Answers

26. What is NULL in PHP?

In PHP, Null is defined as a special data type that can have only one value, i.e., NULL. A variable of the data type NULL is a variable that will have no value assigned to it. 

27. Define “print” in PHP?

The print() function in PHP outputs one or multiple strings. 

28. How will PHP and HTML interact?

It is possible to generate an HTML through the PHP scripts, and it is possible to pass pieces of data from HTML to PHP.

29. How to define a constant in PHP?

A constant in PHP is an identifier for a simple value. We cannot change the value cannot during the script. 

A valid constant name usually starts with a letter or underscore. 

30. Differentiate between “echo” and “print” in PHP?

echo has no return value.The print has a return value of 1.
echo can take multiple parameters.Print can take one argument.

PHP Interview Questions and Answers

31. What is the unset() function?

The unset() function is used for variable management. It makes a variable undefined.

32. Differentiate between $message and $$message?

$message is mainly used to store variable data.$$message is used to store a variable of a variable.
The data that is stored in the $message is fixed.The data stored that is in the $$message can be changed dynamically.

33. Differentiate between PHP constants and variables?

A constant in PHP is an identifier or a name for the simple value.The variable in PHP is a name or symbol that stands for value and is used to store values like characters, string, numeric, character string, and memory addresses. 
We can skip writing a dollar ($) sign before a constant.A variable has to be written with the dollar ($) sign.
They cannot be redefined or undefined.We can redefine each path individually in the case of variables.

34. What is the function ‘imagetypes()’?

In PHP, the imagetypes() function is an inbuilt function that is used to return the image types that are supported by the PHP inbuilt installed library.

35. Can you list the ways to define a constant in PHP?

PHP constants are defined as a  name or identifier that cannot be changed during the execution of the script.  We can define the PHP constants in two ways:

  1. Using const() function
  2. Using define() function

PHP Interview Questions and Answers

36. Can you list some of the constants in PHP and their purpose?

  1. METHOD_ – It usually represents a class method name.
  2. _LINE_ – It mainly represents the current line number of a file.
  3. _CLASS_ – It will return the class name as it was declared.
  4. _FILE_ – It defines the full path and filename of a file. If it is used inside an include, then it will return the name of the included file.
  5. _FUNCTION_ – It typically represents a function name.

37. Which library is needed to use the image function?

GD library is required to execute image functions.

38. Define magic constants in PHP?

The Magic constants in PHP are defined as the predefined constants that get changed on the basis of their usage. They usually start with a double underscore (__) and end with a double underscore. They are the same as the other predefined constants, but they change their values with the context; hence they are called magic constants.

39. Define break and continue statement?

The break statement in PHP breaks the execution of the current for, do-while, while, switch, and for-each loop. If we use a break inside the inner loop, then it breaks the execution of the inner loop only. The break keyword will immediately end the execution of a loop or switch structure.

The continue statement in PHP  is mainly used to continue a loop. It will continue the current flow of a program and then skip the remaining code at the given condition. We use the continue statement within the loop and switch control structure when you immediately want to jump to the next iteration.

40. Name the functions used to get the image’s properties, such as size, width, and height?

The functions used are:

  1. getimagesize() for size
  2. imagesx() for width
  3.  imagesy() for height

PHP Interview Questions and Answers

41. How to make single-line and multi-line comments in PHP?

The PHP single line comment is made as follows:

  1. Using # (Unix Shell-style single line comment)
  2. Using // (C++ style single line comment)

In PHP, we can comment on multiple lines. You have to enclose all lines within /* */. 

42. How will PHP and Javascript interact?

Typically, PHP and Javascript can’t directly interact because PHP is the server-side language, and Javascript is the client-side language. But, you can exchange the variables as PHP can generate Javascript code that is to be executed by a browser, and it is able to pass specific variables back to PHP through the URL.

43. How to handle failures in execution with include() and require() functions?

If the function require() is not able to access the file, then it results in a fatal error.

The “include() function” will give a warning, and the PHP script will continue to execute.

44. Name the different loops in PHP?

  1. For
  2. While
  3. Do-while
  4. for each.

45. Define constructor and destructor in PHP?

The PHP Constructor can be defined as a special type of function of a class that is gets executed automatically when an object of that class is initiated or created. Constructor is also known as magic function as in PHP, and the magic method usually starts with two underscore characters.

A destructor in PHP is invoked when an object is destructed, or a script is stopped or exited. When you create a __destruct() function, PHP will call this function automatically at the end of the script. The destruct function will start with two underscores (__)!.

PHP Interview Questions and Answers

46. Differentiate between require() and require_once()?

require() one file includes more times.require_once() one file includes only one times.

47. Why do we use the count() function in PHP?

The count() function in PHP will count the elements in an array of “the properties” in an object. It will return the number of elements that are present in an array.

48. How to display text with a PHP script?

<!--?php echo "Method 1";
print "Method 2"; ?-->

49. Why do use the header() function in PHP?

In PHP, the header() function is an inbuilt function that is used to send a raw HTTP header. The HTTP functions can be defined as those functions that manipulate the information sent to the client or a browser by a Web server before any other output has been sent.

50. Is it possible to set an infinite execution time for PHP script?

Yes, it is possible by adding set_time_limit(0) at the beginning of the PHP script, one can set the infinite execution time for a PHP Script. 

Note: Note: This method works if you are allowed to change a PHP configuration by the Hosting Server.

PHP Interview Questions and Answers

51. What is the isset() function?

The isset() function in PHP will check whether a variable is set, which means that it should be declared and is not NULL. This function has to return true if the variable exists and is not NULL; it returns false otherwise.

52. What is the PHP error ‘Parse error in PHP – unexpected T_variable at line X?

It is a PHP syntax error that expresses a mistake at the line x stops parsing and executing the program.

53. List the different types of errors available in Php?

  1. E_WARNING– It gives a Run-time warning that will not cause script termination.
  2. E_PARSE– It is a compile-time parse error.
  3. E_NOTICE– It is a Run time notice that is caused due to an error in code.
  4. E_CORE_ERROR– A Fatal error that occurs during PHP initial startup.
  5. E_USER_ERROR– A User-generated error message.
  6. E_ERROR– It is a  fatal error that causes the script termination.
  7. E_USER_WARNING– A User-generated warning message.
  8. E_USER_NOTICE- It is a User-generated notice message.
  9. E_STRICT– Run-time notices.
  10. E_RECOVERABLE_ERROR– It is a Catchable fatal error that indicates a dangerous error.
  11. E_ALL– It Catches all errors and warnings.
  12. E_CORE_WARNING– They are the Warnings that occur during PHP initial startup.
  13. E_COMPILE_ERROR– It is a Fatal compile-time error that indicates a problem with a script.

54. Define PHP parameterized functions?

Parameterized functions in PHP are defined as the functions with parameters. One can pass any number of parameters within a function. These passed parameters will act as the variables inside the function. They are usually specified inside the parentheses, i.e., after the function name.

55. How to export data into an Excel file?

The most common way is to get data into a format that is supported by Excel. For example, we can write a .csv file, choose, for example, a comma as a separator between the fields, and then open the file with Excel.

PHP Interview Questions and Answers

56. Define the syntax for the ‘foreach’ loop?

The foreach loop is mainly used to loop through the values of an array. It loops over an array, and each value for the present array element is assigned to an  $value, and the array pointer is usually advanced by one to go to the next element in an array.


foreach(array as value)
// Code to be executed

57. Define PHP variable-length argument function?

PHP is able to support variable-length argument functions.  This means that one can pass 0, 1, or n number of arguments in a function. To do so, you have to use three ellipses (dots) before the argument’s name. 

58. What is the use of the function file_get_contents()?

The file_get_contents() is used to read a file into a string. This function is usually the preferred way to read the content of a file into the string. It uses memory mapping techniques to enhance performance.

59. List the different types of Array in PHP?

  1. Indexed Array – It is an array with a numeric index called the indexed array. Here, the values are stored and accessed in a linear fashion.
  2. Multidimensional Array – It is an array that contains one or multiple arrays called a multidimensional array. Here, the values are accessed by using multiple indices.
  3. Associative Array – It is an array with strings as the index called the associative array. It will store the element values in association with the key values rather than in strict linear index order.

60. Can you list some of the PHP array functions?

  1. count()
  2. sort()
  3. array()
  4. array_change_key_case()
  5. array_chunk()
  6. array_reverse()
  7. array_search()
  8. array_intersect()

61. Why do we use the function mysql_pconnect()?

The mysql_pconnect() function in PHP opens a persistent MySQL connection. This function will return the connection on success or FALSE, and it returns an error on failure. We can hide an error output by adding an ‘@’ in front of a function name.



62. Can you tell the differences between single-quoted string and double-quoted string?

Single-quoted stringDouble-quoted string
These strings are treated almost literally.These strings will replace the variables with their values and interpreting certain character sequences.

63. Differentiate between indexed and associative array?

Indexed arrayAssociative array
They are used to identify things by their position.They have strings as keys and behave more like a two-column table.
This array holds the element in the indexed form, which is defined by a number.This array holds elements with name.

64. List some of the functions in PHP?

  1. preg_split() – This function operates exactly like the split() function,  except those regular expressions are accepted as the input parameters for the pattern.
  2. ereg() – This function is used to search a string that is specified by string for a string specified by the pattern; it returns true if the pattern is found, or else it returns false.
  3. preg_match() – The function searches a string for a pattern, and it will return true if the pattern exists, or else false.
  4. ereg() – This function is used to search a string specified by string for a string specified by a pattern; it will return true if the pattern is found, or else false.
  5. split() – This function is used to divide a string into different elements; the boundaries of each element are based on occurrences of a pattern in a string.

PHP Interview Questions and Answers

65. List some of the PHP string functions?

  1. strlen()
  2. strtolower()
  3. strtoupper()
  4. ucfirst()
  5. lcfirst()
  6. ucwords()
  7. strrev()

66. Differentiate between and PHP?

The programming type is compiled.The programming type is scripted.
Development languages: VB.NET C#/C# .NET F#Development languages: PHPDevelopment tools: Visual Studio, MonoNetBeans, PhpStrom, any other editorServer OS: WindowsLinux, Mac, Windows(limited)The syntax of ASP.NET is the same as that of the visual basic.The syntax is similar to c and c++ languages.

67. List the ways to include files in PHP?

PHP allows to include files so that the page content can be reused again. We have two ways to add the file in PHP.

  1. Include: It only generates a warning (E_WARNING, and continues the execution of a script).
  2. Require: Iy usually generates a fatal error (E_COMPILE_ERROR, and it stops the execution of a script).

68. Why do we use session and cookies in PHP?

A session in PHP is mainly used to store the data on the server rather than the computer of a user. A Session identifier or SID can be defined as a  unique number that is used to identify each and every user in a session-based environment.

Cookies in PHP are mainly stored in the browser in the form of a  text file format. It stores a limited amount of data. It allows only  4kb[4096bytes]. Sessions are usually stored on the server-side.

69. Define setcookie() function in PHP?

A setcookie() function in PHP is used to define a cookie that is to be sent along with the rest of the other  HTTP headers. A cookie is mainly used to identify a user. A cookie is usually defined as a small file that the server will embed on the user’s computer. Each and every time a similar computer requests a page with the browser, it sends the cookie too.


bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path     
[, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] ) 

70. Differentiate between mysqli_fetch_object() and mysqli_fetch_array()?

It will collect the first single matching record.It will collect all the matching records from the table in the array.

PHP Interview Questions and Answers

71. How to retrieve a cookie value?

echo $_COOKIE [“user”]; 

72. Define overloading and overriding in PHP?

The PHP Function overloading is mainly used to dynamically create methods and properties. The dynamic entities are then processed by the magic methods that are used in a class for different action types. Function overloading consists of the same function name, and that function aims to perform the different tasks according to the number of arguments.

In the PHP function overriding, both the parent and the child classes must have a similar function name and a similar number of arguments. It is usually used to replace the parent method in the child’s class. The main aim of overriding is to change the behavior of the parent class method. Two methods with a similar name and a similar parameter are known as overriding.

73. Define PHP session_start() and session_destroy() function?

The session_start() function in PHP usually creates a session or resumes a present one based on the session identifier that is passed through a GET or POST request or passed through a cookie. When we call the session_start() or when the session auto starts, PHP calls the open and read session save handlers.

The session_destroy() in PHP will destroy all the data that is associated with the current session. It will not unset any of the global variables that are associated with the session or will unset the session cookie. In order to use session variables again, we need to call session_start().

74. How to create a database using PHP and MySQL?


  1. One has to establish a connection to the MySQL server from the PHP script.
  2. If the connection is successful, then you need to write a SQL query to create a database and store it in the string variable.
  3. Lastly, execute the query.

75. What is the syntax to open a file in PHP?

The PHP fopen() function is mainly used to open the file or n URL and returns the resource. It usually accepts two arguments they are: $filename and $mode.


resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )  

76. Why do we use callback in PHP?

A callback function in PHP is a function that is passed as an argument into another function.

77. In PHP, how to read a file?

The PHP will provide different functions to read data from a file. The different functions will allow you to read all the file data, read the data line by line, and read the data character by character.

The PHP file read functions are:

  1. fread(): This function is mainly used to read the data of the file. It usually requires two arguments, namely, file resource and file size.
  2. fgets(): This function is usually used to read a single line from the file.
  3. fgetc(): The function is mainly used to read a single character from the file. To get all the data by using the fgetc() function, use the!feof() function within the while loop.

78. Define a lambda function in PHP?

A PHP lambda function can be defined as an anonymous PHP function that is stored in a variable and is passed as an argument to the other functions or methods. 

79. How to write in a file and delete a file in PHP?

The PHP fwrite() and fputs() functions are usually used to write the data into a file. To write the data into a file, you have to use w, r+,x, w+, x+, c, or c+ mode.


int fwrite ( resource $handle , string $string [, int $length ] )  

The unlink() function is usually used to delete a file in PHP.


bool unlink ( string $filename [, resource $context ] )

80. How to encrypt passwords using PHP?

This uses the PHP Password API that is available in version 5.5. 0 and above. Encryption of the password: In order to generate a hash from a string, we usually use a password_hash() function. This  password_hash() function will create a new password hash of the string by using one of the available hashing algorithms.

PHP Interview Questions and Answers

81. List the different categories of errors in PHP?

  1. Fatal Errors: These are the critical errors. These errors are caused due to the immediate termination of the script.
  2. Notices: These are usually non-critical errors. They are not displayed to the users.
  3. Warnings: These are serious errors, but they don’t result in any script termination. By default, these errors are usually displayed to the user.

82. Define Type hinting in PHP?

The Type hinting in PHP means providing hints to the function to only accept the specified data type. In a technical word, we say that Type Hinting is a method by which we can force a function to accept the required data type. In PHP, we use type hinting for Array, Object, and callable data types.

83. How can we stop the execution of the PHP script?

The exit() function is mainly used to stop the execution of the PHP scripts.

84. How to send email in PHP?

The mail() function that is used to send email in PHP is given below:

bool mail($to,$subject,$message,$header); 

85. How can we upload files in PHP?

We use the move_uploaded_file() function to upload file in PHP:

bool move_uploaded_file ( string $filename , string $destination )    

86. Explain PHP explode() function and PHP split() function?

The PHP split() function splits the string into an array by the regular expression.

The PHP explode() function is usually used to break a string into an array.

87. How to access the data sent through the URL with the GET method?

To access the data sent through the GET method, we can use the $_GET array like this:
$variable = $_GET["var"]; this now contain 'value'

88. How to check if a given variable is empty?

To check whether a variable has a value or not, we should use the empty() function.

89.  Differentiate between runtime exception and compile-time exception?

Runtime exceptionCompile-time exception
Runtime Exception is usually thrown if an error that is only be found on runtime occurs.It is an exception that is thrown at compile time.

90. Define a Persistent Cookie?

A persistent cookie can be defined as a cookie that is usually stored in a cookie file permanently on a browser’s computer. Persistent cookies are usually less secure as users open cookie files to see the cookie value.

PHP Interview Questions and Answers

91. How important is Parser in PHP?

A Parser is software that is used to converts source code into something more easily and more efficiently and is understandable by a computer. The Zend PHP interpreter consists of a PHP parser, so it can check the code and report any possible syntax errors.

92. Name the encryption functions in PHP?

CRYPT() and MD5()

93. In PHP, how to remove escape characters from a string?

The stripslashes function will allow us to remove escape characters before apostrophes in a string.

94. How to escape data before storing it in the database?

The addslashes function will allow us to escape data before storage into a database.

95. How to find the number of rows returned in the result set?

The function mysqli_num_rows() is used to return the number of rows in a result set.

96. Can you name the function that gives the number of affected entries by a query?

mysqli_affected_rows() will return the number of entries that are affected by an SQL query.

97. How to remove the HTML tags from data?

The strip_tags() function is used to clean a string from the HTML tags.

98. Define $GLOBALS, $_SERVER?

$GLOBALS is defined as an associative array that includes references to all the variables that are currently defined in the global scope of a script.

$_SERVER can be defined as an array that includes the information created by a web server like paths, headers, and script locations.

99. How to find the error when there is a problem uploading a file?

The $_FILES[‘userfile’][‘error’] consists of the error code that is associated with the uploaded file.

100. Define $_FILES?

$_FILES can be defined as an associative array that is composed of items that are sent to the current script through the HTTP POST method.

We hope our PHP Interview Question and Answers were of some help to you. You can also check our .NET Interview Questions and Answers, which might be of some help to you. Thank you.

Recommended Articles