PHP Interview Questions

Php Interview Questions & Answers – Most Asked Questions

PHP developers are the most sought-after professionals across the globe. PHP is a widely used open-source programming language suitable to create dynamic web applications & mobile APIs. Here is a complete list of PHP interview questions & answers to help you grab the dream job of PHP developer. If you are looking to build your career in this field, you need to get through the rigorous interview process.

Websites help communicate the products as well as services to a wide range of online customers. Unless it is dynamic & user-friendly, businesses can’t survive the competition in this digital age. Hence, there is a huge demand for PHP developers worldwide.

It’s the right time to grab this amazing opportunity. Grow and build your career as a Php web developer.

Following are top basic PHP interview questions & answers to help beginners get an understanding of the nature of questions they may encounter during the interview.

PHP Interview Questions for Freshers:

PHP Interview Questions & Answers for freshers:

Freshers can follow the below mentioned questions:

1. What is a PHP program?

The PHP program is a server-side scripting language to develop websites and web applications. It stands for Hypertext Pre-processor. In fact, it has inbuilt support for working hand in hand with MySQL. PHP programming language is used over 20 million websites & applications

The following are the popular frameworks in PHP.

#1. Laravel

#2. Code Igniter

#3. Symfony

#4. CakePHP

#5. YII Framework

#6. Zend Framework

#7. Phalcon

#8. Fuel PHP

3. What is a variable in PHP?

A PHP variable is the name of a memory location that holds data.

It is a temporary storage.

The Content Management System is used to make small changes in the site. The changes can be a text, image or adding a new page. The popular content management systems in PHP are:

#1. WordPress

#2. Drupal

#3. Magento

#4. TYPO3

5. Explain about variable length in PHP?

PHP supports variable-length argument function. In fact, you can pass ‘n’ number of arguments in a function. Likewise, there should be 3 ellipses (dots) before the argument name to pass arguments.

Follow the PHP code. It can be helpful.

<?php

function add(…$numbers)

{

$sum = 0;

foreach ($numbers as $n)

{

$sum += $n;

}

return $sum;

}

echo add(2, 3, 3, 4);

?>

Output: 12

6. How many data types are there in PHP?

There are 8 primitive data types in PHP.

It includes integer, float, String, Booleans, Array, Object, resource & NULL.

7. What are the ways to define constants in PHP?

PHP constants include name or identifier. They can’t be changed during the execution of the script.

PHP constants are defined in two ways:

  • Using define () functionUsing const () function

8. What are the Traits in PHP?

Traits are a mechanism for code reuse in single inheritance. It is intended to reduce some limitations of single inheritance. It enables developers to reuse sets of methods without any control in several independent classes living in different class hierarchies.

9. What are PHP string functions?

Following are PHP string functions:

  • strtolower()strtoupper()ucfirst()lcfirst()ucwords()strrev()strlen()

10. What is Multiple Inheritance in PHP?

It is the property of the Object-Oriented Programming language. Moreover, child class or subclass can inherit the properties of the multiple parent classes as well as superclasses. However, PHP doesn’t support multiple inheritances. But by using interfaces in PHP or using Traits in PHP instead of classes, we can implement it.

11. What are SQL Injections?

SQL injection destroys the database.

It permits hackers to spoof identity & tamper with existing data.

12. How do you contact the MySQL database with PHP?

Following steps have to be followed to contact MySQL database with PHP:

#1. Create Database

#2. Form a folder in docs

#3. Create a Database connection file in PHP

#4. Make a new PHP file to check your Database connection

#5. Run it!.

#6. Create Database Connection

#7. MySQLi Procedural Query

#8. Connect MySQL Database with PHP using PDO.

13. What is the importance of Session in PHP?

Session in PHP is used to store and pass information from one page to another temporarily. Firstly, it allows accessibility of data across the various pages of an entire website. Secondly, it makes a file in a temporary directory on the server to store session variables. Thirdly, it creates a unique user id for each browser to recognize the user in order to avoid conflict between multiple browsers.

14. What are the PHP functions?

The real power of PHP lies in its function. In fact, there are more than 100 built-in PHP functions. Apart from this, users can create their custom functions as well.

15. How can you download & upload a file in PHP?

Steps to download the file: 

Step-1: Initialize a file URL to the variable.

Step-2: Create a cURL session.

Step-3: Declare a variable and store name where download file will save

Step-4: Use the basename() function to return the file basename if the file path provided

Steps for file upload: 

Step-1: Configure The “PHP. Ini” File.

Step-2: Check if the File already exists.

Step-3: Limit File Type.

Step-4: Complete Upload File PHP Script.

16. What does MVC stand for & what does each component do?

MVC stands for Model-View-Controller. It is an application that comprises of three interconnected parts. These parts develop modern user interfaces.

Purpose of each component:

#1. Model: It is data used by the program.

This can be a database or a file.

#2. View: It means displaying objects within an application so that the user can see.

#3. Controller: It updates both models & views by accepting user input.

17. What are getters & setters and why are they important?

“Getters” & “Setters” are object methods that allow you to control access to certain class variables/properties.

A “getter” allows you to retrieve a given property whereas “setter” allows you to set the value of a given property.

18. What is an Array in PHP?

An array is a data structure that stores one or more similar types of values in a single value.

There are basically three types of arrays in PHP:

##1. Indexed or Numeric Arrays: The values are stored linearly

##2. Associative Arrays: It is an array with a string index where each value is assigned a specific key

##3. Multidimensional Arrays: They contain single or multiple arrays within. And they can be accessed via multiple indices.

19. What is the difference between echo and print?

Echo as well as print method are the methods which print the output in the browser. However, there is a difference between these two methods. Echo does not return any kind of value after printing the output.

Moreover, echo is faster whereas the print method is slower. That is so because the print method returns boolean value after printing the output.

20. What is the difference between mysqli_connect and mysqli_pconnect?

mysqli_connect() This function is used for searching any existing persistence connection. And if no persistent connection is found, this function creates a new database connection. Then it automatically terminates the connection at the end of the script.

mysqli_pconnect() This function is used for making a persistence connection. This is done with the help of a database. In this function, the database does not terminate even when the script ends.

We hope you might have got an overview of the PHP interview questions for freshers. Freshers need to have in-depth knowledge of the basics of PHP to crack the interview. Usually, an interviewer may ask other random questions.

Note: Don’t panic at the Interview. Stay calm and be honest with your answer. No one is perfect entirely.  

Now, have a look at the PHP interview questions for the experienced candidate.

PHP Interview Questions for Experienced:

PHP Interview Questions & Answers for Experienced : 

Experienced professionals can follow them.

21. How to explode a string using multiple delimiters(“,” and “|” )?

$string=’php, interview | questions , and | answers ‘;

$output = preg_split( “/(\,|\|)/”, $string );

print_r($output );

*/

Array ( [0] => php [1] => interview [2] => questions [3] => and [4] => answers )

*/

22. How to convert a UNIX Timestamp to Formatted Date?

$timestamp=’1463721834′;

echo date(“Y-m-d h:i:s A”, $timestamp);//2016-05-20 05:23:54 AM

23. How can we remove a key and value from an associative array?

$arrayData = array(0=>’one’,1=>’two’,3=>’three’);

print_r($arrayData);

unset($arrayData[1]);

/*

Array ( [0] => one [3] => three )

*/

24. How to detect requests from Search Engine Bots?

if(strstr(strtolower($_SERVER[‘HTTP_USER_AGENT’]), “googlebot”))

{

/*This request is from search engine bots */

}

25. List all files/directory in one directory?

if ($handle = opendir(‘D:\softwares’))

{

while (false !== ($entry = readdir($handle)))

{

if ($entry != “.” && $entry != “..”)

{

echo “$entry”;

}

}

closedir($handle);

}

26. How to re-index an array?

$arrayData = array(0=>’one’,1=>’two’,3=>’three’,66=>’six six’,7=>’Seven’);

$newArray = array_values($arrayData);

print_r($arrayData);

/*

* Array ( [0] => one [1] => two [3] => three [4] => six six [5] => Seven )

*/

27. Why do we use Extract() in PHP?

The extract () function imports variables into the local symbol table from an array.

Following is the syntax:

extract(array,extract_rules,prefix)

Let’s see the examples:

<?php

$varArray = array(“course1” => “PHP”, “course2” => “JAVA”,”course3″ => “HTML”);

extract($varArray);

echo “$course1, $course2, $course3”;

?>

28. What Are Different Ways To Get The Extension Of A File In PHP?

There are following two ways to retrieve the file extension.

  1. $filename = $_FILES[‘image’][‘name’];

$ext = pathinfo($filename, PATHINFO_EXTENSION);

  1. $filename = $_FILES[‘image’][‘name’];

$array = explode(‘.’, $filename);

$ext = end($array);

29. What is PDO in PHP?

PDO is an abbreviation of PHP Data Object. It provides core class, database & specific drivers. In addition to this, it provides a vendor-independent, lightweight, data-access abstraction layer

30. State what Is The Use Of Urlencode() And Urldecode() In PHP?

The use of Urlencode() is to encode a string before using it in a query part of the URL.

The use of Urldecode() is to decode the encoded string.

31. Which PHP Extension Helps To Debug The Code?

Xdebug uses the DBGp debugging protocol for debugging. It is adaptable to a variety of situations. 

To sum up, we hope the above-mentioned PHP interview questions and answers will help to enhance your performance during the PHP job interview. Prepare these basic Php interview questions to improve your chances of selection.

Comments are closed.