PHP interview questions and answers ๐Ÿ‘‡

  1. PHP Interview Questions
  2. Advanced PHP Interview Questions


PHP Interview Questions

1.

What is NULL?

โ†‘

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.

2.

How do you define a constant in PHP?

โ†‘

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

3.

What are break and continue statements?

โ†‘

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.

4.

How would you compare objects in PHP?

โ†‘

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

5.

Explain constructor and destructor in PHP.

โ†‘

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.

6.

What are include() and require() functions?

โ†‘

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.

7.

What are PHP parameterized functions?

โ†‘

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.

8.

What is an array?

โ†‘

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

9.

What are indexed array?

โ†‘

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.

10.

What are associative array?

โ†‘

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.

11.

What is htaccess?

โ†‘

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.

12.

What is Persistent Cookie?

โ†‘

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.

13.

What are Traits?

โ†‘

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.

14.

What is PEAR in php?

โ†‘

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

15.

What are explode() and implode() functions?

โ†‘

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.

16.

What are type casting and type juggling?

โ†‘

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.

17.

What are substr() and strstr()?

โ†‘

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.

18.

What is garbage collection?

โ†‘

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.

19.

What is URL rewriting?

โ†‘

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.

20.

What is PDO?

โ†‘

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.

21.

What is Multidimensional array?

โ†‘

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

1.

What is a session?

โ†‘

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.

2.

What are cookies in PHP?

โ†‘

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.

3.

What is overloading and overriding in PHP?

โ†‘

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.

4.

What are GET and POST in PHP?

โ†‘

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.

5.

What is a lambda function?

โ†‘

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

6.

What are PHP Magic Methods?

โ†‘

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.

7.

What is Type hinting?

โ†‘

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

8.

What is run time exception?

โ†‘

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.

9.

Is multiple inheritance supported in PHP?

โ†‘

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

10.

How can you pass a variable by reference?

โ†‘

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.

11.

What is the scope of a variable?

โ†‘

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

12.

What are magic constants in PHP?

โ†‘

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.