PHP Interview Questions and Answers

Last updated on Mar 24, 2024
  • Share
PHP Interview Questions

PHP earlier stood for Personal Home Pages, but now it stands for Hypertext Pre-processor. PHP is a server-side scripting language that is used for building web pages and applications. We have just added more questions in our question bank for PHP interview questions. This language includes many Frameworks and CMS for creating large websites. Even a non-technical person can create web pages or apps using PHP CMS or frameworks. PHP supports various frameworks and CMS that are used to develop applications.

Before getting confused with the plethora of PHP developer interview questions, you need to ensure the basics. This question will help you to understand the core basics of PHP and make you even more confident enough to jump onto the advanced questions.

Quick Facts About PHP
What is the latest version of PHP? 8.0.3, released on 4th March 2021
Who is the developer of PHP? Rasmus Lerdorf
What language does PHP use? C language
PHP License It is a BSD-style license which does not have the "copyleft" and restrictions associated with GPL.
Key points about PHP:
  • It runs on various platforms, including Windows, Mac OS X, Linux, Unix, etc.
  • It is compatible with almost all servers, including Apache and IIS.
  • It supports many databases like MySQL, MongoDB, etc
  • It is free, easy to learn and runs on the server-side.
  • PHP can operate various file operations on the server

Note: This is a list of the most frequently asked PHP interview questions. Please have a good read and if you want to download it, it is available in a PDF format so that you can brush up your theoretical skills on this subject.

Top PHP Developer Interview Questions

Here in this article, we will be listing frequently asked PHP Interview Questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q81. Write a program to get second highest number in an array using PHP?
Answer
Example

function displaySecondHighest($arr, $arr_size)
{      
    if ($arr_size < 2)
    {
        echo(" Invalid Input ");
        return;
    }  
    $first = $second = PHP_INT_MIN;
    for ($i = 0; $i < $arr_size ; $i++)
    {
        if ($arr[$i] > $first)
        {
            $second = $first;
            $first = $arr[$i];
        } else if ($arr[$i] > $second &&
            $arr[$i] != $first)
            $second = $arr[$i];
        }
    if ($second == PHP_INT_MIN)
        echo("There is no second largest element\n");
    else
        echo("The second largest element is " . $second . "\n");
}
 
// Here is your Array
$arr = array(12, 35, 1, 10, 34, 1);
$n = sizeof($arr);
displaySecondHighest($arr, $n);
 
Output:

34

Q82. What are the advantages of PHP?
Answer
  • Open source
  • Robust functions
  • Centralized built-in database
  • Extensive community support
Q83. What is the latest version of PHP and Mysql?
Answer

PHP 7.4 is the latest version of PHP and 8.0 is the newest version of Mysql.

Q84. What is the difference between GET & POST ?
Answer

These both methods are based on the HTTP method.

GET POST
The GET method transfers data in the form of QUERY_STRING. Using GET it provides a $_GET associative array to access data The post method transfer form data by HTTP Headers. This POST method does not have any restrictions on data size to be sent. With POST method data can travel within the form of ASCII as well as binary data. Using POST, it provides a $_POST associative array to access data.
Q85. How can we make a constant in PHP?
Answer

With the help of define(), we can make constants in PHP.

Example

define('DB_NAME', 'bestInterviewQ')

Q86. What is the difference server side and browser side validation?
Answer

We need to add validation rules while making web forms. It is used because we need to take inputs from the user in the right way like we need the right email address in the email input field. Some time user did not enter the correct address as we aspect. That's why we need validation.

Validations can be applied on the server side or the client side.

Server Side Validation

In the Server Side Validation, the input submitted by the user is being sent to the server and validated using one of the server-side scripting languages like ASP.Net, PHP, etc.

Client Side Validation

In the Client Side Validation, we can provide a better user experience by responding quickly at the browser level.

Q87. How to create and destroy cookies in PHP?
Answer

A cookie is a small file that stores on user's browsers. It is used to store users information. We can create and retrieve cookie values in PHP.

A cookie can be created with the setcookie() function in PHP.

Example

Create Cookie

$cookie_name = "username";

$cookie_value = "Umesh Singh";

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

Update Cookie

$cookie_name = "username";

$cookie_value = "Alex Porter";

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");

Delete Cookie

setcookie("username", "", time() - 3600);

Q88. What is the difference between fopen() and fclose()?
Answer

 

These both are PHP inbuilt function which is used to open & close a file which is pointed file pointer.

S.no fopen() fclose()
1. This method is used to open a file in PHP. This method is used to close a file in PHP. It returns true or false on success or failure.
2. $myfile = fopen("index.php", "r") or die("Unable to open file!"); $myfile = fopen("index.php", "r");
Q89. How to get last inserted id after insert data from a table in mysql?
Answer

mysql_insert_id() is used to get last insert id. It is used after insert data query. It will work when id is enabled as AUTO INCREMENT

Q90. What is str_replace()?
Answer

It is used to replaces some characters with some other characters in a string.

Suppose, we want to Replace the characters "Umesh" in the string "Umesh Singh" with "Sonu"

echo str_replace("Umesh","Sonu","Umesh Singh");

Top 10 PHP Interview Questions

Here you will find the list of Top PHP interview questions, which were written under the supervision of industry experts. These are the most common questions and usually asked in every interview for the role of the PHP Developers.

  • What are Design Patterns in PHP?
  • What is the difference between abstract class & interface in PHP.
  • What is traits & how it can we used?
  • What is the use of .htaccess file?
  • What is the difference between MyISAM and InnoDB?
  • What is the difference between Public Private and Protected?
  • What is Constructor and Destructor?
  • What is final class and final method in PHP?
  • What is a static member function?
  • What is function Overloading and Overriding in PHP?
Reviewed and verified by Umesh Singh
Umesh Singh

My name is Umesh Singh having 11+ experience in Node.JS, React JS, Angular JS, Next JS, PHP, Laravel, WordPress, MySQL, Oracle, JavaScript, HTML and CSS etc. I have worked on around 30+ projects. I lo...