PHP Interview Questions [1 – 15] you should know

Given below is the list of the most popular PHP interview questions which are commonly asked in the interviews.

PHP Interview Questions (1). What is PHP?

PHP stands for Hypertext Preprocessor. PHP is one of the popular server-side scripting languages for developing a web application. It is used by embedding HTML for creating dynamic content, communicating with a database server, handling sessions, etc.

PHP Interview Questions (2). What are the common uses of PHP?

  • It can handle forms, i.e. gathering data from files, save data to a file, through email you can send data, return data to the user.
  • It performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
  • Access cookies variables and set cookies.
  • You can add, delete, modify elements within your database with the help of PHP.
  • Using PHP, you can restrict users to access some pages of your website and also encrypt data.

PHP Interview Questions (3). How to execute a PHP script from the command line?

Use the PHP Command Line Interface (CLI) to execute a PHP script and specify the file name of the script in the following way:

php filename.php

PHP Interview Questions (4). Is PHP a case-sensitive language?

PHP is partially case-sensitive. The variable names are case-sensitive but function names are not. If you define the function name in lowercase and call them in uppercase, it will still work. User-defined functions are not case sensitive but the rest of the language is case-sensitive.

PHP Interview Questions (5). What are the different types of PHP variables?

There are 8 data types in PHP which are used to construct the variables:

  • Integers − are whole numbers, without a decimal point, like 4195.
  • Doubles − are floating-point numbers, like 3.14159 or 49.1.
  • Booleans − have only two possible values either true or false.
  • NULL − is a special type that only has one value: NULL.
  • Strings − are sequences of characters, like ‘PHP supports string operations.’
  • Arrays − are named and indexed collections of other values.
  • Objects − are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
  • Resources − are special variables that hold references to resources external to PHP.

PHP Interview Questions (6). How do you define a constant in PHP?

To define a constant you have to use the define() function and to retrieve the value of a constant, you have to simply specify its name. If you have defined a constant, it can never be changed or undefined. There is no need to have a constant with a $. A valid constant name starts with a letter or underscore.

PHP Interview Questions (7). What are the differences between echo and print?

Both echo and print methods print the output in the browser but there is a difference between these two methods.

echo does not return any value after printing the output and it works faster than the print method. 

print method is slower than the echo because it returns the boolean value after printing the output.

PHP Interview Questions (8). What are the uses of explode() and implode() functions?

explode() function is used to split a string into an array and implode() function is used to make a string by combining the array elements.

PHP Interview Questions (9). Which function is used in PHP to check the data type of any variable?

gettype() function is used to check the data type of any variable.

echo gettype(true).''; //Boolean
echo gettype(10).''; //Integer
echo gettype(null).''; //NULL
echo gettype('GTechHub').''; //String

PHP Interview Questions (10). How long does a PHP session last?

By default, session data will last for 24 minutes or 1440 seconds in PHP. But you can customize the duration by modifying the value of the gc_maxlifetime directive in the php.ini file.

To set the session time for 30 minutes, open the php.ini file and set the value of the gc_maxlifetime directive as follows,

gc_maxlifetime = 1800

PHP Interview Questions (11). What are the differences between session and cookie?

The session is a global variable that is used in the server to store the session data. When a new session creates the cookie with the session id is stored on the visitor’s computer. The session variable can store more data than the cookie variable.

Session data are stored in a $_SESSION array and Cookie data are stored in a $_COOKIE array. Session values are removed automatically when the visitor closes the browser and cookie values are not removed automatically.

PHP Interview Questions (12). What is a persistence cookie?

A cookie file that is stored permanently in the browser is called a persistence cookie. It is not secure and is mainly used for tracking a visitor for long time.

This type of cookie can be declared as follows,

setccookie ("cookie_name", "cookie_value", strtotime("+4 years");

PHP Interview Questions (13). What is garbage collection?

It is an automated feature of PHP.

When it runs, it removes all session data which are not been accessed for a long time. It runs on “/tmp” directory which is the default session directory.

PHP directives that are used for garbage collection include:

  • session.gc_maxlifetime (default value, 1440)
  • session.gc_probability (default value, 1)
  • session.gc_divisor (default value, 100)

PHP Interview Questions (14). What is the meaning of a final class and a final method?

The final keyword in a method declaration indicates that the method cannot be overridden by subclasses. A class that is declared final cannot be subclassed. This is particularly useful when we are creating an immutable class like the String class. Properties cannot be declared final, only classes and methods may be declared as final.

PHP Interview Questions (15). Name some of the popular frameworks in PHP?

Some of the popular frameworks in PHP are:

  • CakePHP
  • CodeIgniter
  • Yii 2
  • Symfony
  • Zend Framework

Got a question for us? Please mention it in the comments section of the “PHP Interview Questions” blog and we will get back to you.

Scroll to Top