PHP Interview Questions Set 1

PHP Interview questions for freshers

This is the first set of PHP developer inteview questions, these questions are specific to beginners but equally well applicable to other levels as well. This question set will help you not just clear your interviews but also build your knowledge and expertise in the language further.


1) What do you know about PHP?

PHP is a general purpose programming language that is primarily used for Web Development. It is a very popular server-side language that powers a large number of websites. It has number of frameworks and CMS, some of the well established names include Wordpress, Drupal, Joomla, Magento etc.


2) What is a "general purpose scripting language"?
A general purpose programming language is used for building software in a wide range of application domains, for example C/C++, Go, Java, PHP are all general purpose programming languages, whereas a Domain Specific Language is applicable for building software in specific domains only for eg. SQL, Action Script etc.


3) What is the current major release of of PHP?

As per the PHP's official website php.net. The recent major stable release of PHP is PHP 8.1


4) What do the initials PHP stands for?

PHP earlier stood for "Personal Home Page" but now it is "Hypertext Preprocessor" which basically means it processes HTML before it is sent to a web browser.


5) Give a brief background of PHP?

PHP is a popular server-side scripting language, developed by Rasmus Lerdorf. It was first released in 1994, the developer initially created the language for his own Personal Home Page. It was then further improved by other developers and PHP 3.0 was released in 1997 with a range of improved features, enough to support development of an e-commerce site. PHP since those days have a come a long way now. PHP is now a large ecosystem in itself with countless number of developers and resources online.


6) What is the difference between a server-side scripting language and a client side language?

Her's a list of a few differences between a server side scripting language and a client side scripting language.

Server Side Scripting Language Client Side Scripting Language
A server side scripting language is a language specifically designed to write applications to be executed on Web servers. A client side language is one which is designed to write applications to be executed on a client side eg. a web browser
Examples include PHP, NodeJS, Java, C# etc. Example of such languages include Javascript, VB Script
The code is executed on a server and only the response is sent back The entire code is sent to the client for execution

7) Explain the various data types available in PHP?

There various data types in PHP of which 8 are primitive data types. The

  • Integer
    An integer data type holds whole number values ranging from -2,147,483,648 to +2,147,483,648
  • Float
    Unlike integers floating point numbers are numbers with decimal in them, these types are suitable for representing fractional numbers.
  • String
    A data type that is intended to hold a sequence of characters.
  • Objects
    An object is a complex data type in PHP, basically, an instance of a class. Unlike other data types in PHP it must be explicity declared. A class contains the structure and behavior of the object.
  • Boolean
    A boolean data type holds a bit value which can either be true or false.
  • Array
    An array is a primitive data type in PHP which allows us to store multiple elements within a variable with keyed or named/string indexes. Unlike arrays in other languages, arrays in PHP can hold values of different data types.
  • Resource
    A resource in PHP is a special data type that holds a reference to an external resource. For example handles to opened files, database connections, image canvas areas etc. It is created and used only by dedicated functions.
  • NULL
    A null type represents a variable with no value. It is the only possible value of type null.

8) What are the various ways to define a string in PHP?

A string literal in PHP can be defined in four ways, using single quotes, double qoutes, heredoc and nowdoc syntax. An example of each is given below.

Defining a string with double quotes:

<?php
// Single Quoted String
$string = 'this is a single quoted string"

Defining a string with double quotes:

<?php
// Double Quoted String
$string = "this is a double quoted string"

Defining a string using heredoc syntax:

<?php
$string = <<<END
This is how you define a string using heredoc syntax in PHP.
Note that there must not be any space on the line before the keywor "END".     
END;

Defining string using nowdoc syntax:

$string = <<<'EOD'
This is how you define a string using nowdoc syntax.
Note the identifier that follows is enclosed in single quotes.
Unlike heredoc strings in nowdoc are not escaped
EOD;
9) What is an associative array in PHP and how it is defined?

Associative arrays are arrays which have strings as their keys instead of a numbered index. These arrays are similar to indexed arrays and are used to store key value pairs. PHP has various inbuilt functions that makes it seamless to work with arrays. We define associative using named indexes as shown below:

<?php
$array = ['foo' => 'bar'];

10 What do you know about type juggling in PHP?

The ability of PHP to automatically determine the data type of a variable based on its current value is known as type juggling.. For eg. if a variable has been assigned a string value it becomes a string, similarly if the same variable is assigned an integer value, it becomes an integer.