PHP interview questions and answers 👇

  1. PHP Interview Questions
  2. Advanced PHP Interview Questions

PHP Interview Questions

What is NULL?

View answer

Null is a special data type which can have only one value: NULL. A variable of data type NULL is a variable that has no value assigned to it.

How do you define a constant in PHP?

View answer

A constant is an identifier (name) for a simple value. The value cannot be changed during the script.

A valid constant name starts with a letter or underscore (no $ sign before the constant name).

What are break and continue statements?

View answer

The break statement can also be used to jump out of a loop.

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

How would you compare objects in PHP?

View answer

To compare objects in PHP, you can use either the comparison operator (==) or identity operator (===).

Explain constructor and destructor in PHP.

View answer

In object oriented programming terminology, constructor is a method defined inside a class is called automatically at the time of creation of object. Purpose of a constructor method is to initialize the object. In PHP, a method of special name __construct acts as a constructor.

A destructor is called when the object is destructed or the script is stopped or exited. If you create a __destruct() function, PHP will automatically call this function at the end of the script.

What are include() and require() functions?

View answer

The Include() function is used to put data of one PHP file into another PHP file. If errors occur then the include() function produces a warning but does not stop the execution of the script i.e. the script will continue to execute.

The Require() function is also used to put data of one PHP file to another PHP file. If there are any errors then the require() function produces a warning and a fatal error and stops the execution of the script i.e. the script will continue to execute.

What are PHP parameterized functions?

View answer

PHP Parameterized functions are a piece of code that can be used repeatedly in a PHP program. Any number of parameters can be passed inside a parameterized function after the name of the function, inside the parentheses.

What is an array?

View answer

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

What are indexed array?

View answer

PHP indexed array is an array which is represented by an index number by default. All elements of array are represented by an index number which starts from 0. PHP indexed array can store numbers, strings or any object.

What are associative array?

View answer

Associative array refers to an array with strings as an index. Rather than storing element values in a strict linear index order, this stores them in combination with key values.

What is htaccess?

View answer

The .htaccess (Hypertext Access) file is an Apache distributed server configuration file. One can use the .htaccess file to set server configurations for a specific directory. This directory can be the root directory of the website or another subdirectory where the .htaccess file is created in order to enable extra features for that subdirectory.

What is Persistent Cookie?

View answer

A persistent cookie is a cookie that is stored in a cookie file permanently on the browser's computer. Temporary cookies are stored only in the browser's memory.

What are Traits?

View answer

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

What is PEAR in php?

View answer

PEAR (PHP Extension and Application Repository) is a framework and distribution system for reusable PHP components.

What are explode() and implode() functions?

View answer

The implode() function takes an array, joins it with the given string, and returns the joined string. The explode() function takes a string, splits it by specified string, and returns an array.

What are type casting and type juggling?

View answer

PHP type casting allows you to convert a value from one type to another. Use a type casting operator to cast a value to the desired type.

Type juggling means dealing with a variable type. In PHP a variable type is the context in which it is used.

What are substr() and strstr()?

View answer

The substr() is a built-in function of PHP, which is used to extract a part of a string. The substr() function returns a part of a string specified by the start and length parameter.

The strstr() function searches for the first occurrence of a string inside another string. Note: This function is binary-safe.

What is garbage collection?

View answer

The garbage collector is triggered whenever 10,000 possible cyclic objects or arrays are currently in memory, and one of them falls out of scope.

The collector is enabled by default in every request.

What is URL rewriting?

View answer

URL rewriting is a technique that allows you to use a URL that actually goes to another URL within the same website. This is used mostly in dynamic database generated websites so the final URL is more Search Engine friendly.

What is PDO?

View answer

PDO in PHP (PHP Data Objects) is a lightweight, consistent framework for accessing databases in PHP. Database-specific features may be exposed as standard extension functions by any database driver that implements the PDO interface.

What is Multidimensional array?

View answer

A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep can be difficult to manage.

Advanced PHP Interview Questions

What is a session?

View answer

A PHP session is used to store data on a server rather than the computer of the user. Session identifiers or SID is a unique number which is used to identify every user in a session based environment.

What are cookies in PHP?

View answer

A cookie in PHP is a small file with a maximum size of 4KB that the web server stores on the client computer. They are typically used to keep track of information such as a username that the site can retrieve to personalize the page when the user visits the website next time.

What is overloading and overriding in PHP?

View answer

Method overloading occurs when two or more methods with same method name but different number of parameters in single class. PHP does not support method overloading. Method overriding means two methods with same method name and same number of parameters in two different classes means parent class and child class.

What are GET and POST in PHP?

View answer

Get and Post methods are the HTTP request methods used inside the

tag to send form data to the server. HTTP protocol enables the communication between the client and the server where a browser can be the client, and an application running on a computer system that hosts your website can be the server.

What is a lambda function?

View answer

A Lambda function is an anonymous PHP function that can be stored in a variable and passed as an argument to other functions/methods.

What are PHP Magic Methods?

View answer

Magic methods are special methods which override PHP's default's action when certain actions are performed on an object. All methods names starting with __ are reserved by PHP. Therefore, it is not recommended to use such method names unless overriding PHP's behavior.

What is Type hinting?

View answer

With Type hinting we can specify the expected data type (arrays, objects, interface, etc.) for an argument in a function declaration.

What is run time exception?

View answer

A RuntimeException is an exception that might also occur in code that is written and configured 'perfectly'. Such an exception may be caused by input from the end user, or an error in (the communication with) an external system. When a RuntimeException is thrown, it should not require a fix in the code.

Is multiple inheritance supported in PHP?

View answer

PHP doesn’t support multiple inheritance but by using Interfaces in PHP or using Traits in PHP instead of classes, we can implement it.

How can you pass a variable by reference?

View answer

When variables are passed by reference, use & (ampersand) symbol need to be added before variable argument. For e.g. : function( &$x ).

Scope of both global and function variable becomes global as both variables are defined by same reference.

What is the scope of a variable?

View answer

The scope of a variable is the part of the script where the variable can be referenced/used.

What are magic constants in PHP?

View answer

Magic constants are the predefined constants in PHP which get changed on the basis of their use. They start with double underscore (__) and ends with double underscore.