PHP Interview Questions and Answers (2024) | Set-2
Last Updated :
12 Jul, 2025
In this article, you will learn PHP Interview Questions and Answers that are most frequently asked in interviews. Before proceeding to learn PHP Interview Questions and Answers Set 2, first we learn the complete PHP Tutorial and PHP Interview Questions and Answers.
PHP Interview Questions and AnswersPHP is the Hypertext Preprocessor. It is a server-side scripting language designed specifically for web development. It is open-source which means it is free to download and use. It is very simple to learn and use. The files have the extension “.php”.
Pre-requisites
PHP TutorialsPHP Interview Questions and Answers (2023)PHP Interview Questions
1. How can you enable error reporting in PHP ?
If you are getting errors and you don't have idea about those errors then you can use inbuilt error_reporting() function. This function will give you information about those errors where and why it is happening. The best place to include this function is the beginning of your PHP script. You can also set this function for some specific script or you can set it for all the scripts in your server by editing the php.ini file.
There are various type of errors in PHP but it contains basically four main types of errors.
- Parse error or Syntax Error: It is the type of error done by the programmer in the source code of the program. Parse errors can be caused dues to non-closed quotes, missing or extra parentheses, non-closed braces, missing semicolon, etc.
- Fatal Error: It is the type of error where PHP compiler understands the PHP code but it recognizes an undeclared function. It means that function is called without the definition of the function.
- Warning Errors: The main reason for warning errors are including a missing file. This means that the PHP function call the missing file.
- Notice Error: It is similar to warning error. It means that the program contains something wrong but it allows the execution of script.
3. What is inheritance in PHP ?
Inheritance in PHP means the child class can inherit all the properties and protected methods from it's parent class and extend keyword is used to define the inheritance.
PHP doesn’t support multiple inheritance but by using Interfaces in PHP or using Traits in PHP instead of classes we can implement it.
The trait is a type of class which enables multiple inheritance. Classes, case classes, objects, and traits can all extend no more than one class but can extend multiple traits at the same time.
- Unlink() function: The unlink() function is an inbuilt function in PHP which is used to delete a file. The filename of the file which has to be deleted is sent as a parameter and the function returns True on success and False on failure. The unlink() function in PHP accepts two-parameters filename and context.
- Unset() function: The Unset() function is an inbuilt function in PHP which is used to remove the content from the file by emptying it. It means that the function clears the content of a file rather than deleting it. The unset() function not only clears the file content but also used to unset a variable, thereby making it empty accepts a single parameter variable.
8. If x = 10 and y = "10" then what does the condition x === y returns ?
php
<?php
$x = 10;
$y = "10";
var_dump($x === $y);
?>
It will return bool(false).
9. What is Nullable types in PHP ?
This feature is new to PHP, Nullable adds a leading question mark indicate that a type can also be null.
php
function geeks(): ?int {
return null; // ok
}
By default maximum upload file size for PHP scripts is set to 128 megabytes. But you can change it, the maximum size of any file that can be uploaded on a website written in PHP is determined by the values of max_size that can be posted or uploaded, mentioned in the php.ini file of the server. In case of a hosted server need to contact the administrator of the hosting server but XAMPP has interpreters for the scripts written in PHP and Perl. It helps to create a local HTTP server for developers and it provides them full physical and administrative access to the local server. Hence it is the most widely used server and it is very easy to increase the limit on upload files to the desired value in this server.
Use PHP inbuilt function ini_set(option, value) where the parameters are the given configuration option and the value to be set. It is used when you need to override the configuration value at run-time. This function is called from your own PHP code and will only affect the script which calls this function. Use init_set('max_execution_time', 0) when you want to set unlimited execution time for the script.
php
// The program is executed for 3mns.
<?php
ini_set('max_execution_time', 180);
?>
- include() function: This function is used to copy all the contents of a file called within the function, text wise into a file from which it is called. This happens before the server executes the code.
- require() function: The require() function performs same as the include() function. It also takes the file that is required and copies the whole code into the file from where the require() function is called.
- Public Access modifier: This modifier is open to use inside as well as outside the class.
- Protected Access modifier: This modifier is open to use within the class in which it is defined and its parent or inherited classes.
- Private Access modifier: This modifier is open to use within the class that defines it. ( It can’t be accessed outside the class means in inherited class).
The spaceship operator or combined comparison operator is denoted by "<=>". This is a three-way comparison operator and it can perform greater than, less than and equal comparison between two operands.
- This <=> operator offers combined comparison:
- Return 0 if values on either side are equal
- Return 1 if value on the left side is greater
- Return -1 if the value on the right side is greater
16. What is urlencode() and urldecode() ?
- urlencode() function: The urlencode() function is an inbuilt function in PHP which is used to encode the URL. This function returns a string which consists all non-alphanumeric characters except -_. and replaced by the percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.
- urldecode() function: The urldecode() function is an inbuilt function in PHP which is used to decode the URL which is encoded by encoded() function.
The line break can be removed from string by using str_replace() function. The str_replace() function is an inbuilt function in PHP which is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively.
There are three ways of removing an extension from the string. They are as follows
- Using an inbuilt function pathinfo
- Using an inbuilt function basename
- Using a string functions substr and strrpos
19. How to check the value of variable is a number, alphanumeric or empty ?
You can use is_numeric() function to check whether it is a number or not. The ctype_alnum() function is used to check whether it is an alphanumeric value or not and use the empty() function to check variable is empty or not.
Yes it is possible by using the strip_tags() function. This function is an inbuilt function in PHP which is used to strips a string from HTML, and PHP tags. This function returns a string with all NULL bytes, HTML and PHP tags stripped from a given $str.
Similar Reads
PHP Tutorial PHP is a popular, open-source scripting language mainly used in web development. It runs on the server side and generates dynamic content that is displayed on a web application. PHP is easy to embed in HTML, and it allows developers to create interactive web pages and handle tasks like database mana
9 min read
Basics
PHP SyntaxPHP, a powerful server-side scripting language used in web development. Itâs simplicity and ease of use makes it an ideal choice for beginners and experienced developers. This article provides an overview of PHP syntax. PHP scripts can be written anywhere in the document within PHP tags along with n
4 min read
PHP VariablesA variable in PHP is a container used to store data such as numbers, strings, arrays, or objects. The value stored in a variable can be changed or updated during the execution of the script.All variable names start with a dollar sign ($).Variables can store different data types, like integers, strin
5 min read
PHP | FunctionsA function in PHP is a self-contained block of code that performs a specific task. It can accept inputs (parameters), execute a set of statements, and optionally return a value. PHP functions allow code reusability by encapsulating a block of code to perform specific tasks.Functions can accept param
8 min read
PHP LoopsIn PHP, Loops are used to repeat a block of code multiple times based on a given condition. PHP provides several types of loops to handle different scenarios, including while loops, for loops, do...while loops, and foreach loops. In this article, we will discuss the different types of loops in PHP,
4 min read
Array
PHP ArraysArrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficien
5 min read
PHP Associative ArraysAn associative array in PHP is a special array where each item has a name or label instead of just a number. Usually, arrays use numbers to find things. For example, the first item is at position 0, the second is 1, and so on. But in an associative array, we use words or names to find things. These
4 min read
Multidimensional arrays in PHPMulti-dimensional arrays in PHP are arrays that store other arrays as their elements. Each dimension adds complexity, requiring multiple indices to access elements. Common forms include two-dimensional arrays (like tables) and three-dimensional arrays, useful for organizing complex, structured data.
5 min read
Sorting Arrays in PHPSorting arrays is one of the most common operation in programming, and PHP provides a several functions to handle array sorting. Sorting arrays in PHP can be done by values or keys, in ascending or descending order. PHP also allows you to create custom sorting functions.Table of ContentSort Array in
4 min read
OOPs & Interfaces
MySQL Database
PHP | MySQL Database IntroductionWhat is MySQL? MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP. MySQL is developed, distributed, and supported by Oracle Corporation. The data in a MySQL database are stored in tables which consists of columns and rows.MySQL
4 min read
PHP Database connectionThe collection of related data is called a database. XAMPP stands for cross-platform, Apache, MySQL, PHP, and Perl. It is among the simple light-weight local servers for website development. Requirements: XAMPP web server procedure: Start XAMPP server by starting Apache and MySQL. Write PHP script f
2 min read
PHP | MySQL ( Creating Database )What is a database? Database is a collection of inter-related data which helps in efficient retrieval, insertion and deletion of data from database and organizes the data in the form of tables, views, schemas, reports etc. For Example, university database organizes the data about students, faculty,
3 min read
PHP | MySQL ( Creating Table )What is a table? In relational databases, and flat file databases, a table is a set of data elements using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect. A table has a specified number of columns, but can have any number of rows. Creating a
3 min read
PHP Advance
PHP SuperglobalsPHP superglobals are predefined variables that are globally available in all scopes. They are used to handle different types of data, such as input data, server data, session data, and more. These superglobal arrays allow developers to easily work with these global data structures without the need t
6 min read
PHP | Regular ExpressionsRegular expressions commonly known as a regex (regexes) are a sequence of characters describing a special search pattern in the form of text string. They are basically used in programming world algorithms for matching some loosely defined patterns to achieve some relevant tasks. Some times regexes a
12 min read
PHP Form HandlingForm handling is the process of collecting and processing information that users submit through HTML forms. In PHP, we use special tools called $_POST and $_GET to gather the data from the form. Which tool to use depends on how the form sends the dataâeither through the POST method (more secure, hid
4 min read
PHP File HandlingIn PHP, File handling is the process of interacting with files on the server, such as reading files, writing to a file, creating new files, or deleting existing ones. File handling is essential for applications that require the storage and retrieval of data, such as logging systems, user-generated c
4 min read
PHP | Uploading FileHave you ever wondered how websites build their system of file uploading in PHP? Here we will come to know about the file uploading process. A question which you can come up with - 'Are we able to upload any kind of file with this system?'. The answer is yes, we can upload files with different types
3 min read
PHP CookiesA cookie is a small text file that is stored in the user's browser. Cookies are used to store information that can be retrieved later, making them ideal for scenarios where you need to remember user preferences, such as:User login status (keeping users logged in between sessions)Language preferences
9 min read
PHP | SessionsA session in PHP is a mechanism that allows data to be stored and accessed across multiple pages on a website. When a user visits a website, PHP creates a unique session ID for that user. This session ID is then stored as a cookie in the user's browser (by default) or passed via the URL. The session
7 min read