Top 68 PHP Interview Questions and Answers

Here, you will come across some of the most frequently asked questions in PHP job interviews which will help you in your interview preparation.

Let's have a look at some of the most popular and significant PHP questions and answers:

Interview Questions

Most Essential And Frequently Asked Interview
Questions And Answer


Q.1. What is php?


PHP | What is php?

PHP is a popular server site scripting langage stands for "PHP: Hypertext Preprocessor".  It is an open source scripting language, hence it is free to download and use. We can create dynamic and interactive web application, website or web pages wtih the help of PHP. Most of popular website bilt on php, such as Facebook, Google, Yahoo etc.

PHP file contain html, css, javascript and php code. Most used php file extension is ".php".

Why PHP?

  • It is open source, hence it is free to download and use.
  • Easy to learn.
  • It support a wide range of database. (e.g: mysql, Oracal, odbc etc.)
  • Compatatible with most of the server used todays. (e.g: Apache, IIS etc.)
  • It's run on various platforms. (e.g: Windows, Linux, Max OS X etc.)

PHP Syntex

PHP code start with "<?php" php opening tag and end wtih "?>" php closing tag. PHP code must be inside php opening and closing tags.

Your code here...

echo "Hello PHP";

Q.2. How to get the client IP address in PHP?


$_SERVER['REMOTE_ADDR'] id use to get the IP address of the user.

View Detials

Q.3. What are the main error type in php?


Error type in php

There are three main error type in php

  1. Notice Error
  2. Warning Error
  3. Fatal Error
View Detials

Q.4. What is session in php?


Session in php

A session is a global variable which is used to store users/sensitive information on the server. Each session register a unique session id which is use to fetch stored value. Session store data as an associative array. Once a session variable is created then it is used across multiple page. By default, session variables last until the user closes the browser or leave the site otherwise most of the server terminate the session variable if you are not active more than 24 minutes on the site.

View Detials

Q.5. What is the use of header() function in php?


header() function

  1. The header() function in php is use to sends a raw HTTP header to a client.
  2. With the help of header() function we can Change page location, set timezone, set caching control, etc.
  3. It does not return any value means it is a void type
  4. It accept three parameter(i.e.: string, replace and http_response_code)
  5. It is important to Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
View Detials

Q.6. What type of inheritance supported by php?


PHP | Inheritance

PHP mainly supports two types of inheritance:

  • Single inheritance
  • Multilevel inheritance

Q.7. What is the difference between unlink and unset function?


unlink() and unset()

The unlink() and unset() function in php have same functionality (i.e.: delete) but the difference is in the type of data on which they are applied to perform such delete functionalities.

unlink() used to delete files from directory where as unset() is used to unset or delete the variable.

View Detials

Q.8. What are the difference between Notify url and Return url?


PayPal | Notify url and Return url

Both of these are used in PayPal payment gateway integration.

Notify url is used by paypal to send response regarding current transaction in other word notify about the current transaction done in your site.

<input type="hidden" name="notify_url" value="">

By paypal: “Instant Payment Notification (IPN) is a message service that automatically notifies merchants of events related to PayPal transactions.”

Return url is use to specify the returning url after current transaction done. It is a redirect page url which is specified by the developer. Something link a page having "Thank you for your payment" message.

<input type="hidden" name="return" value="">

Q.9. What are default session time in PHP?


PHP | What are default session times in PHP?

By default, session variables last until the user closes the browser or leaves the site otherwise most of the server terminates the session variable if you are not active for more than1440 sec or 24 minutes on the site.

Q.10. How can we get the current session id in PHP?


session_id() function

There is a function session_id() in php which is use to get and/or set the current session id.

string session_id ([ string $id ] )

for getting session id in php we have to use session_id() function without parameter.

echo session_id()

if we want to set session id then we need to pass a parameter inside session_id() function. For this purpose we needs to be called session_id() function before session_start()

Q.11. how many ways we can retrieve the data in the result set of MySQL using PHP?


PHP-MYSQL |  Fetch Recoards From MySql table

There are 4 ways:

  1. mysqli_fetch_row() - Get a result row as an enumerated array
  2. mysqli_fetch_array() - Fetch a result row as an associative array, a numeric array, or both
  3. mysqli_fetch_object() - Fetch a result row as an object
  4. mysqli_fetch_assoc() - Fetch a result row as an associative array
View Detials

Q.12. What are the difference between mysqli_fetch_object and mysqli_fetch_array?


PHP-MYSQL |  mysqli_fetch_object () VS  mysqli_fetch_array()

mysqli_fetch_object()  fetch a result row as an object where as mysqli_fetch_array()  fetch a result row as an associative array, a numeric array, or both.

View Detials

Q.13. What are the difference between mysqli_fetch_assoc and mysqli_fetch_array?


PHP-MYSQL |  mysqli_fetch_assoc() VS  mysqli_fetch_array()

mysqli_fetch_assoc() function fetch a result row as an associative array where as mysqli_fetch_array() function fetch a result row as an associative array, a numeric array, or both.

View Detials

Q.14. How can we find or count number of elements in an array?


PHP | Count number of elements in an array

There are two ways to find the number of elements in an array:

1. sizeof($arr), This function is an alias of count()

echo sizeof($arr);

2. count($arr)

echo count($arr);


Q.15. What are the predefined classes in PHP?


Predefined classes in PHP are:

  1. Directory
  2. stdClass
  3. __PHP_Incomplete_Class
  4. exception
  5. php_user_filter

Q.16. How can we find the number of rows in a result set using PHP?


PHP | Find the number of rows in a result set

Bellow code is use for the same

$sql = "SELECT * FROM table1";
$result = mysql_query($sql, $db_link);
$num_rows = mysql_num_rows($result);
echo $num_rows;


Q.17. How can we know the number of days between two given dates using PHP?


Bellow code is used for the same

$date1 = date(‘Y-m-d’);
$date2 = ‘2006-08-15’;
$days = (strtotime($date1) – strtotime($date2)) / (60 * 60 * 24);
echo $days;


Q.18. How can we increase the execution time of a PHP script?


PHP | max_execution_time directive in php.ini file

By changing the value of  max_execution_time in the php.ini file

max_execution_time = 30; // in second
View Detials

Q.19. What is the maximum size of a file that can be uploaded using PHP and how can we change this?


PHP | The maximum size of a file that can be uploaded using PHP

The default maximum size of a file that can be uploaded using PHP is 2MB.

upload_max_filesize = 2M;

and we can change the maximum size by changing the value of upload_max_filesize in our php.ini file.

upload_max_filesize = 5M;

Q.20. What are cookies in PHP?


PHP | What are cookies in PHP?

A cookie is a small piece of information store by the browser on the client location and sent to the server with every request. It stores a limited amount of data about 4kb(i.e.: 4096 bytes).

View Detials

Q.21. What is the difference between $name and $$name?


$name and $$name both are variables but with a single difference that is $name is just a normal php variable where as $$name is known as reference variable. It allow you to use $name variable's value as a variable for getting $$name variable value. 

View Detials

Q.22. What is the difference between explode and split?


The explode() and split() both function is use to split a string into an array. But the difference is explode() function splits a string into array by string where as Split function splits string into array by regular expression.

View Detials

Q.23. How do you define a constant in php?


Constant in PHP

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

define("GREETING", "Welcome to");
View Detials

Q.24. How we can increase default timeout of session ?


PHP | Increase default session  timeout

The "session.gc_maxlifetime" is use to modify the default timout of session.

server should keep session data for AT LEAST 1 hour by using the below code. It increase session default timeout by 1 hour.


    //3600 is use for 1 hour

    ini_set('session.gc_maxlifetime', 3600);



Q.25. How to avoid errors in PHP ?


PHP | How to avoid errors in PHP?

We can use PHP inbuilt function error_reporting(0) to avoid all errors in PHP file.





Q.26. What is difference between require() and require_once() ?


PHP | require() and require_once()

Both require() and require_once() is use to include a file but the difference is required_once() function checks if the file already included or not where  require() function does not check.

(same for include_once() and include())

Q.27. What is difference between require() and include() ?


What is difference between require() and include() ?

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

(same for require_once() and include_once())

Q.28. What are superglobal/global variables ?


PHP | What are superglobal/global variables?

Superglobal is out of scope limitation. These variables can be accessed from any function, class, or file without doing anything special such as declaring any global variable, etc. These variables store information in an array. Commonly they are used to store and get information from one page to another in an application.

Below is the list of superglobal variables available in PHP:

  • $_SERVER
  • $_GET
  • $_POST
  • $_COOKIE
  • $_FILES
  • $_ENV

Q.29. How to convert a string to array ?


PHP | How to convert a string to an array?

string to array

There is a function in PHP called explode() which is used to convert any string to an array. 

View Detials

Q.30. How to convert an array to string ?


PHP | How to convert an array to a string?

array to string

By using implode function we can convert any array to string.  In implode function first parameter is separator which specifies what to put between the array elements. Default is "" (an empty string)

View Detials

Q.31. What are scopes of variable in php?


PHP | What are the scopes of variables in PHP?

Scopes define the visibility of a variable. In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be used.

There are three scopes available in PHP

  1. global
  2. local
  3. static
View Detials

Q.32. What are data types in php?


PHP | What are data types in PHP?

Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:

  • String
  • Integer
  • Float (floating-point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

Q.33. How we can check data type of any variable in php ?


PHP | Check data type in php | gettype() | var_dump()

By using gettype() function we can check the data type of a variable.

we can also use var_dump() function for the same.

View Detials

Q.34. How to send mail in PHP ?


Send mail in PHP

The mail() function allows you to send emails directly from a script.

View Detials

Q.35. What is difference between == and === operator ?


PHP | What is difference between == and === operator?

"==" and "==="

The two operators are known as comparison operators. "==" (i.e. equal)  and === (i.e. identical) both are use to check values are same or not. The difference between the two is that '==' should be used to check if the values of the two operands are equal or not whereas, '===' checks the values as well as the type of operands.

View Detials

Q.36. How can you enable errors in PHP ?


Enable errors in PHP

There are two ways to enable error reporting in your PHP scripts:

1. You can add the following function in the the script:


2. You can add the following option in your website's php.ini file:

error_reporting = E_ALL


Q.37. What is difference between strstr() and stristr() function ?


strstr() and stristr()

strstr() and stristr() both are use to find first occurence of a string, but the differences are strstr( ) is case sensitive where as stristr( ) is case insensitive.

View Detials

Q.38. What is difference between echo and print?


PHP |What is the difference between echo and print?

PHP echo and print both are used to display the output in PHP. Both can be used with or without parenthesis. The differences are small

  • echo has no return value while print has a return value of 1, so it can ve used in expressions.
  • echo can take multiple parameters where as print can take only one argument.
  • echo is marginally faster than print.
View Detials

Q.39. What is :: operator and in what case we use it in php?


PHP | :: operator | scope resolution operator

This operator is known as the scope resolution operator. And this operator is used to access the static members of the class. To access static members of a class we do not need to create an object/instance of the class.

View Detials

Q.40. How to redirect a page in PHP?


PHP | Redirect

PHP header() function is used to redirect a page to another page. It supplies raw HTTP headers to the browser and redirects it to another location. The redirection script should be at the very top of the page to prevent any other part of the page from loading. The target is specified by the Location: header as the argument to the header() function. After calling this function the exit() function can be used to halt parsing of the rest of the code.

header(location:; // This will redirect to


Q.41. How to stop execution of PHP script ?


PHP | Stop the execution of php script | exit() funtion

By using exit() function we can stop the execution of php script.

View Detials

Q.42. Why we use isset in PHP?


PHP | isset()

The isset () function is used to check whether a variable is set or not. It returns true if a variable is set otherwise it returns false.

View Detials

Q.43. How to find current date and time in PHP?


PHP |  date() function

By using the PHP date() function we can find the current date and time.


echo date(“d-m-Y”); //This will print today date.

echo date (“d-m-Y h:i:s”) // This will print today’s date with current time.



Q.44. What is use of htmlentities function in PHP?


PHP | htmlentities()

The htmlentities() function is used to convert all applicable characters to HTML entities.

View Detials

Q.45. What is recursion in PHP?


PHP | Recursion 

PHP also supports recursive function call like C/C++. In such a case, a function calls itself within the function.

View Detials

Q.46. How to upload file in PHP?


PHP | Upload File

By using move_uploaded_file() function we can upload file in PHP.The move_uploaded_file() function moves an uploaded file to a new location.

This function returns TRUE on success or FALSE on failure.

View Detials

Q.47. How we can find last insert id in table?


MySQL | Find last insert id in the table

By using the MySQL function mysqli_insert_id() we can retrieve the last inserted id in the database table.

View Detials

Q.48. How we can remove html tags from string?


PHP | Remove html tags from a string

The strip_tags() function is use to remove any HTML, XML, and PHP tags from a string.

View Detials

Q.49. What is the difference between explode() and implode() function?


 PHP | implode() and explode() function

implode() function is use to convert an array into a string where as explode() function is used to convert an string into an array

View Detials

Q.50. what is Cross-site scripting?


Cross-site scripting (XSS)

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications. XSS enables attackers to inject client-side script into web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same-origin policy.

Q.51. what is list in php?



List is a language construct and is similar to an array. It is used to assign a list of variables in one operation. If you are using PHP 5, then the list values start from a right parameter, and if you are using PHP 7, then the list starts with left parameter.

View Detials

Q.52. what are php magic fuction?


PHP | magic function

In PHP all functions start with __ names are magical functions. Magical functions always live in a PHP class. The definition of the magical function is defined by the programmer itself.

Here are list of magic functions available in PHP

__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo().

Q.53. What is php.ini and .htaccess file?


php.ini and .htaccess file

Both php.ini and .htaccess file is the configuration file.


It is a configuration file for php setting. It is a special file by which you can make changes in PHP settings. php.ini file provide many setting such as changing of the default max_execution_time = 30, error_reporting = E_ALL & ~E_NOTICE, file_uploads = [on/off]  etc.



It is the configuration file for server settings. With the help of the .htaccess file, you can make changes in server settings. It is supported by several web servers, including the popular Apache software which is used by most commercial web hosting providers. These files were first used to control user access on a per-directory basis. It is used to control a couple of things such as error handling, enabling SSL, IP blocking and IP whitelisting, URL redirects and URL rewriting, etc.

Q.54. What is access specifiers in php?


PHP | Access specifiers

An access specifier is a code element that is used to determine which part of the program is allowed to access a particular variable or other information.

View Detials

Q.55. How can we remove duplicate value from an array in php?


PHP | Remove duplicate value from an array 

Duplicate value can remove from an array by using the array_unique function. If two or more array values are the same, the first appearance will be kept and the other will be removed. It takes an input array and returns a new array without duplicate values.

View Detials

Q.56. How will you concatenate two strings in PHP?


PHP | Combine two strings

There are several ways to concatenate two strings in PHP.

Use the concatenation operator [.] and [.=]

There are two string operators that are used for concatenation.

[.]:  Concatenation operator | Used to combine two string values to create a new string.
[.=]: Concatenation assignment operator | Appends the argument on the right side to the argument on the left side.

View Detials

Q.57. How to find the length of a string in PHP?


PHP | Length of a string

There is a string function called strlen() which is used to find the length of a string. It accepts a string that returns the length of the given string. 

View Detials

Q.58. Write a example code showing the nested ternary conditional operator in PHP?


PHP | Nested ternary conditional operator

An example code showing the nested ternary conditional operator in PHP.


$background_color = ($num == 0 ? 'blue' : ($num > 0 ? 'green' : 'red'));


Here we are assigning different strings to the $background_color variable based on a numeric value ($num). 

Q.59. What is the difference between a session and cookies in PHP?


Session V/S Cookies

A cookie is an array of data stored by the browser on the client location and sent to the server with every request. It stores a limited amount of data about 4kb(i.e.: 4096 bytes). It is less secure than a session because it present on the client's computer.

The session is an array of data stored on the server and associated with a given user (usually via a cookie containing an id code). It stores an unlimited amount of data. It is more secure because of stored on the server.

View Detials

Q.60. How can you tell if a number is even or odd without using any condition or loop?


PHP | check whether a number is even or odd

A trick to check whether a number is even or odd without using any condition or loop.

View Detials

Q.61. How will you check if a file exists or not using PHP?


PHP | file_exists( ) Function

The file's existence can be confirmed using the file_exist() function which takes the file name as an argument.

View Detials

Q.62. What will be the output of $a and $b once code bellow is executed?
$a = '1';
$b = &$a;
$b = "2$b";



PHP | output of $a and $b once this code $a = '1'; $b = &$a; $b = "2$b" executed;

Both $a and $b will output a string "21".

View Detials

Q.63. What will be the output of the following statements and why?
var_dump(0123 == 123);
var_dump('0123' == 123);
var_dump('0123' === 123);



PHP | Output of the following statements var_dump(0123 == 123); var_dump('0123' == 123); var_dump('0123' === 123


var_dump(0123 == 123)

This will output bool(false) because the leading 0 in 0123 tells the PHP interpreter to treat the value as octal rather than decimal, so the value of 0123 in decimal is equal to 83. so it is clear that 83 is not equal to 123 and will return false.

var_dump('0123' == 123) 

This will output bool(true) since the string '0123' will automatically be converted to integer 123 and the leading 0 is ignored. Now both are the same so it will return true.

var_dump('0123' === 123) 

This will outputs bool(false) since "===" is an identical operator that checks value as well as data type, so it performs a more strict comparison and does not do the automatic type conversion of string to an integer. Hence will return false.

Q.64. What are the errors in below code? What will be output and how can you fix it?

$arr = array();
$arr['var1'] = array(1, 2);
$arr['var2'] = 3;
$arr['var3'] = array(4, 5);

$test = array();

$test = array_merge($test, $arr['var1']);

$test = array_merge($test, $arr['var2']);

$test = array_merge($test, $arr['var3']);




PHP | Array error fix | array_merge()

The output will be as follows:

array(2) { [0]=> int(1) [1]=> int(2) }

It will also generate two warning messages as follows:

Warning: array_merge(): Argument #2 is not an array
Warning: array_merge(): Argument #1 is not an array
View Detials

Q.65. What will be the value of $x when given statement execute?
$x = 3 + "15%" + "$25"?


PHP | Statement $x = 3 + "15%" + "$25"?

The correct answer is 18.

Here’s why:

PHP is a  loosely typed language, therefore it does automatic type conversion based on the context in which a variable or value is being used.

View Detials

Q.66. Is PHP a case sensitive language?


PHP | Case Sensitive

In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive but all variable names are case-sensitive.

View Detials

Q.67. What is an Interpreted language?


Interpreted language

An Interpreted language executes its statements line by line. Languages such as Python, Javascript, R, PHP, and Ruby is a prime examples of Interpreted languages. Programs written in an interpreted language runs directly from the source code, with no intermediary compilation step.

Q.68. What do you mean by the csrf_token?


The csrf_token is used for protection against Cross-Site Request Forgeries. This kind of attack takes place when a malicious website consists of a link, some JavaScript, or a form whose aim is to perform some action on your website by using the login credentials of a genuine user.