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.

Q11. What is the use of the .htaccess file in php?
Answer

The .htaccess file is a type of configuration file for use on web servers running the Apache Web Server software. It is used to alter the configuration of our Apache Server software to enable or disable additional functionality that the Apache Web Server software has to offer.

Q12. List some array functions in PHP?
Answer

In PHP array() are the beneficial and essential thing. It allows to access and manipulate with array elements.

List of array() functions in PHP

  • array()
  • count()
  • array_flip()
  • array_key_exists()
  • array_merge()
  • array_pop()
  • array_push()
  • array_unique()
  • implode()
  • explode()
  • in_array()
Q13. How to make database connection in PHP?
Answer
Example

$servername = "your hostname";
$username = "your database username";
$password = "your database password";

// Create connection

$conn = new mysqli($servername, $username, $password);

// Check connection

if ($conn->connect_error) {
die("Not Connected: " . $conn->connect_error);
}
echo "Connected";

Q14. What is the difference between require() and require_once()?
Answer

require() and require_once() both are used for include PHP files into another PHP files. But the difference is with the help of require() we can include the same file many times in a single page, but in case of require_once() we can call the same file many times, but PHP includes that file only single time.

Note: From the vast number of php basic interview questions, answering this will help you to align yourself as an expert in the subject. This is a very tricky question that often makes developers nervous.

Q15. List some string function name in PHP?
Answer

PHP has lots of string function that helps in development. Here are some PHP string functions.

  • nl2br()
  • trim()
  • str_replace()
  • str_word_count()
  • strlen()
  • strpos()
  • strstr()
  • strtolower()
  • strtoupper()
Q16. How can we upload a file in PHP?
Answer

For file upload in PHP make sure that the form uses method="post" and attribute: enctype="multipart/form-data". It specifies that which content-type to use when submitting the form.

Example

$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}

Q17. How to download files from an external server with code in PHP?
Answer

We can do it by various methods, If you have allow_url_fopen set to true:

  • We can download images or files from an external server  with cURL() But in this case, curl has been enabled on both servers
  • We can also do it by file_put_contents()
Example

$ch = curl_init();
$source = "http://abc.com/logo.jpg";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "/images/newlogo.jpg";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);

/*-------This is another way to do it-------*/

$url = 'http://abc.com/logo.jpg';
$img = '/images/flower.jpg';
file_put_contents($img, file_get_contents($url));

Q18. How many types of errors in PHP?
Answer

Primarily PHP supports four types of errors, listed below:-

  • Syntax Error
  • Fatal Error
  • Warning Error
  • Notice Error

1. Syntax Error: It will occur if there is a syntax mistake in the script

2. Fatal Error: It will happen if PHP does not understand what you have written. For example, you are calling a function, but that function does not exist. Fatal errors stop the execution of the script.

3. Warning Error: It will occur in following cases to include a missing file or using the incorrect number of parameters in a function.

4. Notice Error: It will happen when we try to access the undefined variable.

Note: The page you are accessing has some of the most basic and complex PHP Interview Questions and Answers. You can download it as a PDF to read it later offline.

Q19. What are the difference between session and cookies in PHP?
Answer

Session and Cookies both functions are used to store information. But the main difference between a session and a cookie is that in case of session data is stored on the server but cookies stored data on the browsers.

Sessions are more secure than cookies because session stored information on servers. We can also turn off Cookies from the browser.

Example

session_start();
//session variable
$_SESSION['user'] = 'BestInterviewQuestion.com';
//destroyed the entire sessions
session_destroy();
//Destroyed the session variable "user".
unset($_SESSION['user']);

Example of Cookies

setcookie(name, value, expire, path,domain, secure, httponly);
$cookie_uame = "user";
$cookie_uvalue= "Umesh Singh";
//set cookies for 1 hour time
setcookie($cookie_uname, $cookie_uvalue, 3600, "/");
//expire cookies
setcookie($cookie_uname,"",-3600);

Q20. What is the difference between file_get_contents() and file_put_contents() in PHP?
Answer

PHP has multiple functions to handle files operations like read, write, create or delete a file or file contents.

1. file_put_contents():It is used to create a new file.

Syntax :

file_put_contents(file_name, contentstring, flag)

If file_name doesn't exist, the file is created with the contentstring content. Else, the existing file is override, unless the FILE_APPEND flag is set.

2. file_get_contents(): It is used to read the contents of a text file.

file_get_contents($filename);

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