Advanced PHP Interview Questions and Answers for Experienced in 2022

These are the mostly asked advanced PHP interview questions and answers for experienced developers in 2022.

1. What are the major features of PHP7?

These are the major features of PHP7 -

  • Improved Performance
  • Less Memory Consumption
  • Many fatal errors are converted to exception
  • The null coalescing operator
  • Return and Scalar types declaration
  • Anonymous Classes


2. What makes PHP7 fast?

PHP7 is influenced by HHVM/Hacklang. It uses Zend Engine 3.0 for better memory consumption and enhance the performance. It has more compact data structures throughout.

3. What is abstract syntax tree?

Abstract Syntax Tree is a tree representation of syntax of programming language.

PHP code -> Tokens -> AST -> Opcodes

4. Write code to get and set cookies in PHP?

PHP setcookie() function is used to set cookies and $_COOKIE superglobal variable is used to get the previously set cookie values.

$username = "test user";
setcookie('username', $username, time() + 60 * 60 * 24 * 30); // Cookie sets for 30 days
if ($_COOKIE['username'] != "")
$uname = $_COOKIE['username'];
echo 'Username cookie value: '.$uname;

5. What is CSRF and how can you prevent this in PHP?

Cross-site request forgery, also known as one-click attack or session riding. This can harm the user's data by modifying them or deleting them. It may attack on the user browsers or internally submits some form.

To prevent such type of attack, we generate a random unique token string and include it as a hidden input in the form. Every time when the form is submitted, the generated unique token is also submitted with each GET & POST form request. In this case, if an attacker tries to generate the form request, the attacker would have to know the token value which in a random unique string and difficult to find.

6. How can you get the latitue and longitude of a given address in PHP?

We can get the latitude and longitude of given address in JSON format by sending request to google map geocode api.

$geocode =  file_get_contents(''.$source_address.'&sensor=false');
$latlong = json_decode($geocode);
$latitude = $latlong->results[0]->geometry->location->lat;
$longitude = $latlong->results[0]->geometry->location->lng;
echo $latitude;
echo '<br/>';
echo $longitude;

7. How to detect different devices in PHP?

By using HTTP_USER_AGENT, we can easily detect the device name.

$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$webos = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$blkberry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

8. What is Null Coalesce Operator?

It returns the first value only if it exists and not set to null otherwise it returns the other value.

$x = statement1 ?? statement2;

In the above syntax, if the statement1 exists and is not null then it returns statement1 otherwise it returns statement2.

9. What is Scalar type declaration?

Scalar type declaration means to specify the type of the variable instead of PHP set it automatically. It is applicable to scalar types : strings, integer, float, boolean.

10. What are the types of Scalar type declaration?

There are two types of scalar type declarations -

  • coercive (default)
  • strict

11. How can you get IP address of user in PHP?

We can find the current user IP address by using global variable '$_SERVER'.

<?php echo $_SERVER["REMOTE_ADDR"]; ?>

12. Give PHP cryptographically randomness functions?

These are the PHP cryptographically randomness functions -


It generates cryptographically secure pseudo random integers.


It generates cryptographically secure pseudo random bytes.

13. What is Closure:call() method?

It binds and calls the closure. It is much faster as compared to bindTo() method.

<?php public Closure::call ( object $newthis [, mixed $... ] ) : mixed ?>

14. Define Spaceship Operator?

Spaceship Operator is used to compare two expressions. For example, $a <=> $b returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b.

15. How to declare strict scalar type declaration?

To use the strict type declaration, we first make the strict mode ON in the desired file. This is done by adding the following code at the top of the file.


16. Define Spaceship Operator?

Spaceship Operator is used to compare two expressions. For example, $a <=> $b returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b.

17. How can we easily set user defined exception handler funtion?

The set_exception_handler() function is used to set the user defined exception handler function. The script stops execution after this function is called.

function setException() {
	try { 
	throw new Exception('An error has occured', 42);
	catch(Exception $e)
		echo 'Exception '.$e->getCode().', '.$e->getMessage().
			 ' in filename '.$e->getFile().' on line number '.$e->getLine();

18. What is the use of fgets() function?

The fgets() function reads one line at a time from a file. It reads until it encounters a new line character (\n) or EOF. The maximum length read is the length specified minus 1 byte.

string fgets ( resource $handle [, int $length ] )

19. Define Memcache

Memcache provides handy procedural and object oriented interface to memcached. It stored the database object in dynamic memory to speeds up websites having large dynamic databases.

20. What is PDO? Which database drive is used to connect to MySQL?

PDO extends for PHP Data Object. It is lightweight, more portable interface for accessing database in PHP. It is a database access layer which makes the developer to write portable code much easier.

To perform database operation using PDO, we need database PDO Driver. PDO_MYSQL database driver is used to connect to MySQL.

21. What is the role of prepare() method?

The prepare() method is used to prepare the update statement for execution by using PDO execute() method.

$emp_update = $database->prepare("UPDATE employees SET first_name= :fname, last_name = :lname WHERE id = :id ");

22. What is the open source language?

Open source language is developed by a community of interested parties. This communities collected suggested corrections and upgrades inputs from the developers and work together to make changes to the language.

23. How a scripting language is different from programming language?

A scripting language is different from the programming language. A programming language is compiled and converted to machine code i.e.(0s and 1s) and then executed within an operating system. But the scripting language do not use compiler, it is interpreted line by line as program is executed.

24. What are the new features in php 8?

The important features of PHP 8 are -

  • Union types
  • Constructor Property Promotion
  • Static return type
  • JIT (Just in Time compiler)
  • Named Arguments
  • Null-safe Operator

25. What are the named arguments in PHP 8?

Named arguments are another new expansion to PHP 8. With named arguments, you would now be able to pass an argument to a function dependent on the parameter name.

26. What is null-safe operator?

The null-safe operator provides safety in method/property chaining when the return value or property can be null.