PHP Interview Questions and Answers

What is PHP?

PHP is a web language based on scripts that allow developers to dynamically create generated web pages.

What is the actually used PHP version?

Version 7.1 or 7.2 is the recommended version of PHP.

How do you execute a PHP script from the command line?

Just use the PHP command line interface (CLI) and specify the file name of the script to be executed as follows:

php -a


What is the correct and the most two common way to start and finish a PHP block of code?

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

<?php Enter code here ?> and <? Enter code here ?>


 Is multiple inheritance supported in PHP?

PHP supports only single inheritance; it means that a class can be extended from only one single class using the keyword 'extended'.

What is the meaning of a final class and a final method?

'final' is introduced in PHP5.

Final Class : - Final class means that this class cannot be extended. .

Final Method :-  Final method cannot be overridden.

 How is the comparison of objects done in PHP?

Two of the many comparison operators used by PHP are '==' (i.e. equal) and '===' (i.e. identical). The difference between the two is that '==' should be used to check if the values of the two operands are equal or not. On the other hand, '===' checks the values as well as the type of operands.

Let me explain more using some examples:

== (equal) :- 

  if("22" == 22){
     echo "YES";
  else {
     echo "NO";

Output: Yes (The code above will print "YES". The reason is that the values of the operands are equal.)


=== (Identical) :-

   if("22" === 22) {
      echo "YES";
   else {
      echo "NO";

Output: No (The result we get is "NO". The reason is that although values of both operands are same their types are different, "22" (with quotes) is a string while 22 (without quotes) is an integer.)

What type of operation is needed when passing values through a form or an URL?

If we would like to pass values through a form or an URL, then we need to encode and to decode them using htmlspecialchars() and urlencode().

What is the main difference between include(), require() , include_once() and require_once()?

include() will throw a warning if it can't include the file, but the rest of the script will run.

require() will throw an E_COMPILE_ERROR and halt the script if it can't include the file.

The include_once() and require_once() functions will not include the file a second time if it has already been included.

How can we connect to a MySQLi database from a PHP script?

$database = mysqli_connect("HOST", "USER_NAME", "PASSWORD");


What is the difference between GET and POST Methods?

GET :-

Appends form-data into the URL in name/value pairs

The length of a URL is limited (about 3000 characters)

Never use GET to send sensitive data! (will be visible in the URL)

Useful for form submissions where a user want to bookmark the result

GET is better for non-secure data, like query strings in Google

GET method should not be used when sending passwords or other sensitive information.

Parameters remain in browser history because they are part of the URL

Url example: page2.php?category=sport



Appends form-data inside the body of the HTTP request (data is not shown is in URL)

Has no size limitations

Parameters are not saved in browser history.

Form submissions with POST cannot be bookmarked

POST method used when sending passwords or other sensitive information.

Url example: page2.php

Is it possible to submit a form with a dedicated button?

It is possible to use the document.form.submit() function to submit the form.

For example:

<input type=button value="SUBMIT" onClick="document.form.submit()">


What is the difference between $_FILES['userfile']['name'] and $_FILES['userfile']['tmp_name']?

 $_FILES['userfile']['name']  represents the original name of the file on the client machine,

 $_FILES['userfile']['tmp_name']  represents the temporary filename of the file stored on the server.

What is the difference between mysqli_fetch_object() and mysqli_fetch_array()?

The mysqli_fetch_object() function collects the first single matching record where mysqli_fetch_array() collects all matching records from the table in an array.

What is session in PHP.

As HTTP is a stateless protocol. To maintain states on the server and share data across multiple pages PHP session are used. PHP sessions are the simple way to store data for individual users/client against a unique session ID. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data, if session id is not present on server PHP creates a new session, and generate a new session ID.

How to initiate a session in PHP?

The use of the function session_start() lets us activating a session.

What is the meaning of a Persistent Cookie?

A persistent cookie is permanently stored in a cookie file on the browser's computer. By default, cookies are temporary and are erased if we close the browser.

What is difference between session and cookie in PHP ?

Session and cookie both are used to store values or data.

cookie stores data in your browser and a session is stored on the server.

Session destroys that when browser close and cookie delete when set time expires.

What is default session time and path in PHP. How to change it ?

Default session time in PHP is 1440 seconds (24 minutes) and Default session storage path is temporary folder/tmp on server.

You can change default session time by using below code

   // server should keep session data for AT LEAST 1 hour
   ini_set('session.gc_maxlifetime', 3600);

   // each client should remember their session id for EXACTLY 1 hour


How to increase the execution time of a PHP script ?

The default max execution time for PHP scripts is set to 30 seconds. If a php script runs longer than 30 seconds then PHP stops the script and reports an error.
You can increase the execution time by changing  max_execution_time  directive in your php.ini file or calling  ini_set(‘max_execution_time’, 300) //300 seconds = 5 minutes function at the top of your php script.

What are different types of errors available in Php ?

There are 13 types of errors in PHP, We have listed all

E_ERROR: A fatal error that causes script termination.

E_WARNING: Run-time warning that does not cause script termination.

E_PARSE: Compile time parse error.

E_NOTICE: Run time notice caused due to error in code.

E_CORE_ERROR: Fatal errors that occur during PHP initial startup.

E_CORE_WARNING: Warnings that occur during PHP initial startup.

E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.

E_USER_ERROR: User-generated error message.

E_USER_WARNING: User-generated warning message.

E_USER_NOTICE: User-generated notice message.

E_STRICT: Run-time notices.

E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error

E_ALL: Catches all errors and warnings.

What is the difference between unset and unlink ?

Unlink: Is used to remove a file from server.
usage:unlink(‘path to file’);

Unset: Is used unset a variable.
usage: unset($var);

What are the difference between echo and print?

echo :-

echo is language constructs that display strings.

echo has a void return type.

echo can take multiple parameters separated by comma.

echo is slightly faster than print.

print :-

print is language constructs that display strings.

print has a return value of 1 so it can be used in expressions.

print cannot take multiple parameters.

print is slower than echo.


Support Us

Subscribe My YouTube Channel

Join Our Telegram Channel & Support Eachother