Latest PHP Interview Questions to Refer for Quick Preparation

Here, we have the latest PHP interview questions for candidates who are sincerely looking for a job in PHP and determined to succeed. Our team has picked PHP questions for all levels starting from freshers to experienced.

Demand for PHP developers is growing consistently. And many IT companies are ready to offer a decent package for the right candidates. However, a lot of freelancing or contract options are also available for them. Large sites like Facebook, Baidu, Wikipedia, VK, and even Twitter are also using PHP for powering their backend services.

Despite so many opportunities, you should prepare hard without being complacent. In this post, we’ve added all relevant PHP interview questions covering most of the important areas of web development. So read them thoroughly, save time, and prepare fast.

Success in a technical interview would not only depend on how much you know but also on what should you know.

Latest PHP Interview Questions.

PHP Interview Questions and Answers.

Top PHP Interview Questions and Answers.

PHP Interview Questions & Answers.

Q-1. What does PEAR signify in PHP?


PEAR is an acronym for PHP Extension and Application Repository. It was Stig S. Bakken who introduced it in 1999 with following features.

  • A structured library of open-sourced code for PHP users.
  • Simple code distribution and package management.
  • A standard style for code written in PHP.
  • The PHP Foundation Classes (PFC).
  • The PHP Extension Community Library (PECL).
  • A website, mailing lists, and download mirrors to support the PHP/PEAR community. Thus, PEAR is a community-driven project with the <PEAR Group> as its governing body.
  • It provides a command-line interface that can come into use for installing the packages on demand.


Q-2. What is the difference between $name and $$name?


$name is a variable whereas $$name is a reference to the variable.

Let’s see an example.

$name "Welcome";

$Welcome "To";

$To "TechBeamers";

echo $name;
/*prints Welcome*/
echo $$name;
/*prints To*/
echo $$$name;
/*prints TechBeamers*/

This code sample prints the following message <WelcomeToTechBeamers>.


Q-3. How do you embed PHP code in an HTML page?


In an HTML page, all PHP code must enclose within either of the three special markup tags recognized by the PHP Parser.

  • <?php PHP code goes here ?>
  • <? PHP code goes here ?>
  • <script language=”php”> PHP code goes here </script>

Amongst these <?php…?> is the most commonly used tag.

We can chain as many statements as required separating them with semicolon as shown below.


  echo "Hello World";

  echo "A second statement";



Q-4. What is the name of scripting engine of PHP?


ZEND Engine 2 is the name of the scripting engine that powers PHP.


Q-5. What is the difference between the GET and POST methods?


Following are the key differences between <GET> and <POST>.

  • GET Method.

    • It submits all the Name-Value pairs as a query string in the URL.
    • This method is not secure and reveals the data transmitted with the URL.
    • The allowed length of the GET string should not exceed 2048 characters.
    • If the Form tag does not contain any method name, GET will take over as default.
    • The payload data is in the text format. It accepts only ASCII values.
    • GET is beneficial for performing data retrieval operation.
  • POST Method.

    • It submits all the Name-Value pairs in the Message Body of the request.
    • Unlike the GET, Post method is secure as the Name-Value pairs do not appear in the location bar of the web browser.
    • Since the payload gets encoded into the request, so it doesn’t show up as part of the URL.
    • There is no restriction on the length of the string (i.e. the amount of data transmitted).
    • In case, the POST method is in use and page refresh happens at the same time, then a prompt will occur before processing the request.
    • If the service associated with the processing of a form has side effects (for example, modification of a database or subscription to a service), the method should be POST.
    • It has no restriction on data usage and permits binary data also.
    • POST is beneficial for performing both insert and update operations.


Q-6. What are Magic functions in PHP?


They are special PHP functions that start with a double underscore <__>. None of these are stand-alone. And it is mandatory to define them inside a Class.

Below are some facts about the Magic functions in PHP.

  • It is the programmer who provides their definition instead of PHP. By allowing the programmer to provide the definition, it enables him to achieve powerful things using these Magic functions.
  • PHP does not allow to call them directly from the code. Instead, the call happens indirectly.

Let’s see an example.

class Animal {
   // height of animal 
   public $height;
   // weight of animal
   public $weight;
   // code
   public function __construct($height, $weight)
      $this->height = $height;  //set the height instance variable
      $this->weight = $weight; //set the weight instance variable

Now, if we instantiate the Animal Class using the following line of code.

Animal obj = new Animal(6, 150);

It will automatically call the <__construct()> function and create an object obj, of the Animal class, with its height as 6 and weight as 150.

Here is the list of some powerful magic functions supported by PHP.

__construct() , __destruct()  ,    __call()   ,  __callStatic()  ,  __get(),  __set()     , __isset()     ,__unset()

,  __sleep()       ,  __wakeup(),   __toString()  , __invoke()    , __set_state() ,  __clone().


Q-7. What is a .htacces file in PHP?


The <.htaccess> is a configuration file used on the servers to run the Apache Web Server software. If this file is present in the directory, then the Apache Web Server will load it into memory and execute.

It acts as a powerful tool to alter the configuration of the Web Server software by enabling/disabling the additional functionality and features that it offers.

These include – 

  • The redirection feature like an occurrence of 404, file not found.
  • More advanced functions such as content password protection or image hotlink prevention.


Q-8. How can we display the output directly to the browser?


In order, to display the output directly to the browser, we have to use the special tags <?= and ?>.


Q-9. What are the different types of errors in PHP?


There are essentially three types of errors in PHP.

  • Notices.

    • These are small, non-critical errors that PHP encounters while executing a script. For instance, accessing a variable that is not defined. By default, PHP does not display these errors to the user. However, the default behavior can be modified.
  • Warnings.

    • These are more severe errors, like attempting to include() a file which does not exist. By default, PHP displays these errors to the user. They do not result in script termination.
  • Fatal Errors.

    • These are critical errors. For example, instantiating an object of a non-existent class, or calling a non-existent function. These errors result in immediate termination of the script. PHP’s default behavior is to display them to the user when it occurs.


Q-10. What is the maximum filesize that PHP allow to upload? How can we increase it?


The standard limit in PHP to upload a file is 2MB. However, we can change this value by making modifications in the php.ini file. We have to alter the value of upload_max_filesize and restart all the services.


Q-11. What is a final class in PHP?


The final class is a unique object-oriented concept which prohibits a class from being inherited. We can use it to protect the methods of the base class from getting overridden by the child classes.

Final Class Example.

final class baseclass
   public function mymethod()  {
      echo  "base class method";

class derivedclass extends baseclass
   public function mymethod() {
      echo   "derived class method";

$c = new derivedclass();

In the above example, we’ve kept the base class as final to prevent it from being derived. However, the second class in the example is trying to extend the base which will cause a compile time error.


Q-12. What is the difference between include() and require()?


The <include()> function throws a warning when file not found error occurs. But the script continues to execute till its end. Let’s see an example.

      echo "Welcome";

In the above example, if the file not found error occurs, the code will lead to a warning and print <Welcome>. It is because a warning in PHP neither stops the execution of the script nor does it blocks the echo command.

The <require()> function gives a fatal error if the specified file is not found and also stop the execution of the script. Let’s see an example.

      echo "Welcome";

The above code will throw a fatal error and stop the script. Even the echo command will also not run in this case.


Q-13. What is the use of var_dump in PHP?


The var_dump function takes one or more variables as arguments and returns structural information about their types and values. It has the following syntax.

var_dump(variable1, variable2,.....variablen);

Here is an example.

       var_dump($m, $n);

Above code displays the following output.



Q-14. What are the differences between echo() and print() methods?


Following are the key differences between echo() and print() statements.

  • Echo.

    • It can accept multiple expressions.
    • Since it does not return any value, so responds a bit faster.
    • Echo statement displays the output on the user screen. Its syntax supports using echo with or without parentheses.
    • Multiple arguments are allowed if separated with <,>.
    • It doesn’t return any value.
  • Print.

    • It cannot accept multiple expressions.
    • Returning a value makes it a little slower than the echo.
    • Print statement displays the output on the user screen. Its syntax supports using print with or without parentheses.
    • Multiple arguments are not allowed.
    • Interestingly, it will always return the value 1.


Q-15. What are visibility keywords in PHP?


We can set the visibility of a Property or Method by prefixing the declaration with the following keywords public, protected, or private.

  • Public specifier signifies that the members are accessible from everywhere within your application.
  • Protected specifier indicates that the members are accessible from within the class, the one that inherits it and from the parent as well.
  • Private specifier restricts that the members should only be accessible from within the class.


Q-16. What is the importance of PHP function htmlentities()?


The <htmlentities()> function ensures that the text is correctly printed into HTML. It does this by converting any character that might be read as part of the HTML markup so that it gets displayed, rather than parsed.

For example, the < and > characters have specific meaning in HTML. They are used as start and end delimiters for HTML tags. If we wish to print them as a part of the text (as the “less-than” and “greater than” characters) rather than getting parsed as a part of the HTML markup, we need to convert them into HTML entities.

HTML entities are special character sequences that represent characters, so they can be printed into the HTML without messing it up. Like HTML entities for the < and > characters look like: “&lt;” and “&gt;”.

The htmlentities() function takes a normal string and converts any character like < and > into their respective HTML entity.

Here is the prototype of this function.

htmlentities(string, flags, character-set, double_encode)

Let’s take an example.

   $str = '<a href="">Go to</a>';
   echo htmlentities($str);

The HTML output of the above code above will look like the one shown below.

&lt;a href=&quot;;&gt;Go to;/a&gt;

However, the browser will render the above code in the following style.

<a href="">Go to</a>


Q-17. What is Type juggle in PHP?


In PHP, mentioning the type of the variable is not required on declaring a variable. The data type is determined implicitly, by the value/context of the variable. If we assign an integer value to a variable $num, then it becomes of type integer, implicitly. If we assign a string value to the variable $str, it becomes of type string.

Let’s take an example.

$num3 = $num1 + $num2

Here, if $num1 is an integer, PHP also treats $num2 and $num3 as integers.


Q-18. What is the use of header() function in PHP?


The header() function sends a raw HTTP header to the client browser. Its rule is to call this function always, before sending the actual output.

For example, we don’t print any HTML element, before using header() function.


Q-19. What is PHP filter and why is it used? Also, list down different PHP filter functions.


The primary use of PHP filter is to validate the data coming from different sources.

  • User input from a form
  • Cookies
  • Web services data
  • Server variables
  • Database query results

Filtering is an important feature for a web application to validate and sanitize the data coming from insecure sources. And the PHP filter extension has many of the functions required for checking user input which makes data validation, easier and quicker.

Following are a few different filter functions supported by PHP.

PHP filter Functions.

  • <filter_has_var()>
    • Checks if a variable of specified input type exists.
  • <filter_id()>
    • Returns the ID number of a specified filter.
  • <filter_var_array()>
    • Get multiple variables and filter them.
  • <filter input()>
    • Get input from outside the script and filter it.
  • <filter_input_array>
    • Get multiple inputs from outside the script and filters them.
  • <filter_list()>
    • Returns an array of all filters it supports.
  • <filter_var()>
    • Get a variable and filter it.


Q-20. How can we get all the properties of a browser in PHP?


We can get the browser properties by using following code snippet.



Summary – Latest PHP Interview Questions and Answers.

We are hopeful that above PHP interview questions would lead you to the door of success. In this post, we’ve tried to touch upon a combination of core PHP concepts, programming and object-oriented constructs and built-in functions. However, if there is anything which is not clear or not covered here, then do write us. We’ll try our best to answer your queries.

Also, we’ll continuously update this blog with more questions in future. If you like to appreciate our efforts, then do share this post across.



The post Latest PHP Interview Questions to Refer for Quick Preparation appeared first on Python, Java, TestNG, Selenium Webdriver Tutorials.