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.

Q71. What is 'This' key word?
Answer

In PHP, the “This” keyword is a reference to a PHP object which has been created by the interpreter for the user containing an array of variables. If “this” keyword is called inside a normal method within a normal class, it returns the object to the appropriate method.

Q72. What is difference between drop table and truncate table?
Answer
Truncate Table Drop-Table
It is a DDL Command This command is used to remove a table from the database.
It is executed using a table lock and the whole table is locked while removing all the records. Using this, all the rows, indexes and other privileges shall also be removed.
The WHERE clause cannot be used with TRUNCATE. By using this command, no DML triggers shall be fired.
It removes all rows within a table. Once started, this operation cannot be rolled back again.
Minimum logging required in the transaction log. Hence, it is more efficient. It is also a DDL command.
This command is used to remove all the data by re-allocating the data pages to store only table data and only using it for recording page deallocations. Removes multiple table data definitions within a database.
Q73. What is difference between __sleep and __wakeup()?
Answer
_sleep() _wakeup()
It is used to return the array of all the variables which need to be saved. It is used to retrieve all the arrays returned by the _sleep() command.
It is executed before the serialize() command. Is executed before the unserialize() command.
Q74. What is the difference between overloading and overriding in PHP?
Answer
function overloading function overriding
The OOPs feature function overloading consists same function name and the function performs various tasks according to the number of arguments. In OOPs feature function overriding, both child and parent classes will have the same function name and a different number of arguments.
Q75. How to remove duplicate values from array using PHP?
Answer
To remove duplicate values from an array using PHP, we have to use the PHP array_unique() function. It removes duplicate values or elements from the array. If our array contains string keys, then this function will keep the first encountered key for every value and will ignore all other subsequent keys.
Example

$var = array("a"=>"best","b"=>"interview","c"=>"question","d"=>"interview");

print_r(array_unique($var));

OUTPUT

Array ( [a] => best [b] => interview [c] => question)

Q76. What is the role of php.ini file?
Answer

The php.ini file is a default configuration file present in applications that require PHP. Developers use it to control different variables such as file timeouts, resource limits and upload sizes.

Q77. List the different types of Print functions available in PHP?
Answer

Here are the different types of print functions available in PHP for developers use:

  • Print() Function
  • Printf() Function
  • Echo() Function
  • Sprintf() Function
  • Print_r() Function
  • Var_dump() Function
Q78. How to send email using php script?
Answer

First, create a sendEmail.php file in web document root and we have to use the below-mentioned script in it to send email using a PHP script. Change the $to_email with a recipient email address, $body and $subject as you require, and $from_email with a sender email address here.

Example

$to_email = "[email protected]";
$subject = "Simple Email Test via PHP";
$body = "Hi,nn This is test email send by PHP Script";
$headers = "From: [email protected]";
if ( mail($to_email, $subject, $body, $headers)) {
     echo("Email successfully sent to $to_email...");
} else {
     echo("Email sending failed...");
}

Q79. Write a program to swap two numbers using PHP.
Answer

We have to use the bellow mentioned syntax to swap two number using PHP, with or without using the third variable.

Example
Without using third variable

<?php echo "Before Swapping:";
$a = 1;
$b = 2;
echo "a = $a<br>";
echo "b = $b<br>";

echo "After swapping:<br>";

$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
echo "a = $a<br>";
echo "b = $b";
?>

With using third variable

<?php echo "Before Swapping:<br>";
$a = 1;
$b = 2;
echo "a = $a<br>";
echo "b = $b<br>";

echo "After swapping:<br>";
$temp = $a;
$a = $b;
$b = $temp;

echo "a = $a<br>";
echo "b = $b<br>";
?>

Q80. Write a program to get LCM of two numbers using PHP?
Answer

Here is the program we can use to get LCM of two number using PHP.

Example

// PHP program to find LCM of two numbers

// Recursive function to
// return gcd of a and b
function gcd( $a, $b)
{
      if ($a == 0)
      return $b;
      return gcd($b % $a, $a);
}

// Function to return LCM
// of two numbers
function lcm( $a, $b)
{
      return ($a * $b) / gcd($a, $b);
}

// Driver Code
$a = 15;
$b = 20;
echo "LCM of ",$a, " and " ,$b, " is ", lcm($a, $b);

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...