PHP Interview Questions For Freshers & Experienced

  • Question 1) What is full form of PHP?
  • Question 2) Php resembles syntax from which programming Language?
  • Question 3) Can we run php script from CLI? Explain how?
  • Question 4) Explain what pear in PHP?
  • Question 5) Enlist some frameworks of PHP?
  • Question 6) What is difference between echo and print?
  • Question 7) How to define constant in PHP?
  • Question 8) Enlist major datatypes supported by PHP?
  • Question 9) What are magic constants in PHP?
  • Question 10) List types of array available in PHP?
  • Question 11) What is T_PAAMAYIM_NEKUDOTAYIM in PHP?
  • Question 12) Explain the difference between == and === operator in PHP?
  • Question 13) What is purpose of @ in Php ?
  • Sharad Jaiswal
  • 11th Dec, 2019
  • 1079 Followers

PHP, a general-purpose language which is released by Rasmus Lerdorf in the year of 1994. In starting, it is the small open source project but advanced with time as more people found it very useful. Now, the question arises, what PHP do? To know that, we gather some important questions which are described below. You will get all your answers by reading them. For now, let’s have some basic introduction about PHP.

PHP, aka "Hypertext Preprocessor" is a server-side scripting language which is designed mainly for web development. The basic functionality of PHP is to manage dynamic content, session tracking, and databases. It is also used in combination with different web template systems, web framework, and web content management system. Moreover, you can use PHP to build many e-commerce websites too. Thus, it is expected that the companies would like to hire developers who have good knowledge in PHP. To shine in interviews, you must answer all the questions asked by interviews. But you need not to worry, for your ease, below are some top collection of PHP interview questions that every interviewer asked. Read them and get a chance to get a great job.

PHP Interview Questions
Updated ON

11th Dec, 2019

No. of Questions

31

Posted In

PHP

Author

Sharad Jaiswal

Key Features of Php

Below are few major features of Php

Open Source

Platform Independent

Secure and flexible

Loosely Typed Language

Interpreted

Object Oriented

Big Community

Easy to Learn

Widely Used

PHP Interview Questions

Q1. What is full form of PHP?

The full form of PHP is Hypertext Preprocessor.

Q2. Php resembles syntax from which programming Language?

The syntax of PHP resembles c, c++ as PHP retains semi-colon, curly braces from it. But when it comes to logic, it can be compared with JavaScript. A lot of functions like loops, conditions in both PHP and JavaScript are similar. Visually C, C++, and PHP are similar.

Q3. Can we run php script from CLI? Explain how?

Yes, the PHP script can be executed from the command line. To execute it, go to the directory where your PHP script file is stored. Open the command line from that directory and run the following command.

PHP filename.php //here filename is the actual name of your PHP script. 

Q4. Explain what pear in PHP?

PEAR (PHP Extension and Application Repository) is a repository of PHP code and software components. Developed by Stig S Bakken in 1999, PEAR is mainly used to reuse existing code, improve code structure. PEAR group is the governing body of PEAR that mainly promotes a standard coding style for PHP developers using PEAR. PHP comes with a PEAR package manager which is used to download the PEAR packages as a tar file. The PEAR framework is divided into three classes which are PEAR library, PEAR core components, PECL packages. The PECL package is like a PEAR package but has the C extensions that are compiled into PHP.

Q5. Enlist some frameworks of PHP?

Some of the popular frameworks in PHP are,

  • Laravel – An open-sourced framework that is used to develop complex applications seamlessly.
  • CodeIgniter – A popular lightweight framework for developing dynamic web applications.
  • Symfony – A PHP MVC framework for developing large scale applications.
  • CakePHP – A easy to learn PHP framework for developing feature-rich websites.
  • Yii – A simple framework for developing modern PHP web applications.
  • ZEND – An object-oriented based PHP framework for developing complex, large-scale web applications.

Q6. What is difference between echo and print?

In PHP, print and echo are both used to print statements but with a small difference.

  • PHP print – It takes only one argument and has a return value of 1. It can used with or without parenthesis.
//example 
<?php 
print "PHP print statement"; 
?> 

  • PHP echo – It can be used to print more than one argument separated by a comma and does not return any values. It can also be used with or without parenthesis. In PHP, echo is faster to execute than print.
//example 
<?php 
$x = 5; 
$y = 5; 
echo $x + $y; 
?>

Q7. How to define constant in PHP?

In PHP, constants are named that stores a value. The value of the constant cannot be changed at runtime and it is global by default.

To define a constant in PHP, the define method is used.

//syntax 
define(name, value, casesensitive) 

Here, is the name of the constant. Value is the constant value. Casesensitive defines whether the constant should behave as case sensitive or not. By default, it is false and not case sensitive.

<?php 
define("CONSTANT", "HELLO!"); 
echo CONSTANT; 
?> 

Q8. Enlist major datatypes supported by PHP?

PHP supports all major data types such as String, Integer, Float or Double, Boolean, Array, Object, NULL, Resource, etc.

Q9. What are magic constants in PHP?

The magic constants in PHP are a set of predefined constants that changes its feature based on where it is used. These constants start and end with a double underscore. The magic constants in PHP are,

  • __LINE__ - It represents the current line number.
  • __FILE__ – It represents the name and path of the file.
  • __DIR__ – It represents the full directory path of the file.
  • __FUNCTION__ – It is used to represent the function name.
  • __CLASS__ – It is used to represent the class name.
  • __TRAIT__ – It is used to represent the trait name.
  • __METHOD__ – It is used to represent the name of the class method.
  • __NAMESPACE__ – It represents the name of the namespace.

Q10. List types of array available in PHP?

An array is used to store more than one value to do various operations. There are three types of array present in PHP.

  • Indexed array – Here the array has sorted values with numeric index.
//syntax to create an indexed array. 
$name = array("one", “two”, “three”, “four”, “five”); 

Here, the values in the array name can be accessed or traversed using its index value starting from 1.

  • Associative array - It is similar to indexed array but the value is assigned in the array with a user-defined key instead of index numbers.
//example to define a associative array 
$name = array("ONE"=>"1", "TWO"=>"2", "THREE"=>"3", "FOUR"=>"4", "FIVE"=>"5"); 

Here, the values “1”, “2”, “3”, “4”, “5” are associated with user-defined keys “ONE”, “TWO”, “THREE”, …. The array values can be accessed or traversed with the key values.

  • Multidimensional array – Here every element in the array can store array within. That is the element in the array is itself an array and sub array within.
//example to create a multidimensional array 
$number = array( array( "one" => "1", "two" => "2", "three" => "3", ), array( “four" => "4", "five" => "5", "six" => "6", ) ); 

Here, each element is another array. The elements within the sub array can be accessed using the index value. For eg: to access “one”, type $number[0][“one”].

Q11. What is T_PAAMAYIM_NEKUDOTAYIM in PHP?

The Paamayin nekudotayim(::) is the scope resolution operator in PHP for identifying integer by specifying the namespace. It is the Hebrew word for the double colon. The T_PAAMAYIM_NEKUDOTAYIM is the syntax error PHP throws when it expects the scope resolution operator(::)

Q12. Explain the difference between == and === operator in PHP?

= = and = = = are both the comparison operator used in PHP. = = is called as the equal operator that is used to check if the values are equal in both side of the operand. = = = is called as the identical operator that is used to check values as well as the data types of the operands are equal or not.

//example for = = 
   <?php 
if("2" == 2) { 
   echo "YES"; 
} 
?> 

It prints YES as the values are equal even though the datatype is not.

//example for = = = 
if("1" == “1”) { 
   echo "YES"; 
} 

It prints YES as both the value and the datatype are equal.

Q13. What is purpose of @ in Php ?

@ in PHP is used to control the expression. It is the error control operator. When @ is appended before an expression, any error generated by that expression will be ignored. Using @ is not a good practice as it doesn’t eliminate error but hides it.

//example 
<?php   
$value = @$array_PHP[$key];   
?> 
//If the key doesn’t exist, the error will not be displayed as the error control operator is appended before the expression. 
 

Q14. What is difference between strstr() and stristr() ?

strstr() and stristr() are both used to find the first occurrence of the string except for one difference. That is strstr() is case sensitive and stristr() is not.

//example for strstr() 
strstr(“prograMminG”, “M”);

Here the output is Mming as the case matches with each other.

//example for stristr() 
stristr(“prograMming”, “m”);

Here the output is Mming even though the case doesn’t match as the method is case insensitive.

Q15. How to get count elements of an array in PHP?

The count() function in PHP is used to return the size of the array passed to this method.

//syntax 
count(array, mode); 

Here array is the array name to be passed. Mode specifies whether to count the values in multi-dimensional array or not. By default, the value is 0 and doesn’t count the sub array length in the multi-dimensional array. You can change it to 1 to count the values of the sub array in multi-dimensional array.

//example  
<?php 
$array_PHP=array("one","two","three"); 
echo count($array_PHP); 
?> 

It returns the value 3.

Q16. How to register a variable in PHP session?

To register a session variable in PHP, Start your session_start() at the beginning of your script. Use the $_SESSION[‘VARIABLE_NAME’] to create and register your session variable. It is a superglobal variable that is accessible by every file in your application.

Eg: $_SESSION[‘user_name’] = “login_name”; 

Note:- You can destroy the session variables using the method session_method();

Q17. What is default time and path of PHP session?

PHP has a session time of 24 minutes which is the default. The default path where session data files are stored in the temporary folder that is, /tmp.

Q18. What is difference between require and include in PHP?

Both the require and include statements are used to include different text, markup, code files into the current PHP file. The difference between require and include method are,

Require statement produces error statement (E_COMPILE_ERROR) when the file is not found and will stop executing the script.

//require() statement example
require("path/to/filename"); //produces error if the file is not found. 

Include a statement only produces the error warning and will continue to run the script without the file.

//include() statement example
include("path/to/filename"); //produces only warning(E_WARNING) when the file is not found. 

Q19. Enlist different types of errors available in PHP?

Errors are nothing but a mistake in the script. PHP has four types of error.

Parse error – It is the error in the syntax of the code. The error arises due to human mistakes. The error is identified by the compiler and fixed by the programmer. The script works fine after it is fixed. Examples of this type are semicolon missing, unclosed quotes or braces, etc.

Example

<?php 
echo "Dog" 
?> 

Note:- In the above code, a semi-colon is missing. So, PHP produces the syntax error.

Fatal error – It occurs when we call an undefined function. PHP understands the code but is unable to execute it.

Example

<?php 
function fun1() 
{ 
echo "Function 1"; 
} 
fun2(); 
?> 

Note:- Here, we define the function 1 but call the undefined function 2. So, PHP produces the undefined function error.

Warning error – As the name implies, it is just a warning. Not an actual error. Examples are missing files, wrong number of arguments, etc.

Example

<?php 
include ("Warning.php"); 
?> 

Note:- Here, the Warning.php is not present int the directory. So, the PHP produces a warning message.

Notice error – It is the same as the warning error but the execution is not stooped here.

Example

<?php   
echo $x;  
?> 

Note:- Here the variable x is not declared or initialized. So, the PHP produces an undefined variable error.

Q20. What are namespaces in PHP?

In PHP, namespaces are used to declare the same function or classes or methods in the different namespaces without getting any error or conflict by encapsulating the code. They are just normal PHP code that is declared using the namespace keyword. It should be declared at the top of the file to use it throughout the file. The namespace provides flexibility while writing code. A namespace can have sub-namespaces within it. A namespace can be declared globally by creating the namespace without any name.

<?php   
Namespace NamspaceName {  
       function namespaceFunction()   
   {  
// Regular PHP code  
   }  
}  
?> 

Q21. How to add redirect in PHP?

Redirection in PHP is used to redirect to a different web page from another page. It can be done using the header() method.

//syntax for header function 
header( $header, $replace, $http_response_code ); 

Here, $header holds the header string. $replace is an optional parameter that indicates that the header should replace the previous header or add a similar header. $http_response_code holds the actual response code.

//example 
<?php  
header("Location: http://www.google.com");  
exit;  
?> 
 

Here, PHP redirects the page to google home page. Exit() or die() after redirection is mandatory as it exits any following code after the redirection.

Q22. What is use of GD library in PHP?

Developed by Thomas Boutell, the GD library is used in the development of web applications. An open-source library used for creating images in the format of JPEG, GIF, and PNG. It is also used to generate charts and graphics too.

Q23. What is cURL in PHP?

The cURL in PHP is a library that is used to make an HTTP request to fetch a URL. The curl_init() creates the session and returns the curl handle. After the initialization, there are many functions in curl to grab the contents of the URL and display it. Curl_exec($ch) grabs the contents of the URL and passes it into a variable to display. Curl_setopt() function is also used to grab the contents in various fashion based on the parameters passed.

//example 
<?php  
$url = "https://www.google.com";  
$curl = curl_init(); //it initializes the session 
curl_setopt($ch, CURLOPT_URL, $url); //grabs the content of the URL and passes it to the variable. 
$result = curl_exec($ch); //the grabbed content is passed to another variable for displaying the result. 
echo $result; 
?> 

Q24. What is composer?

Composer is an application manager that is used to install dependencies and libraries in PHP. Inspired by npm and bundler, the Composer is an open-source cross-platform dependency manager that was released in 2012. The important commands in the composer require to a library, install to install a library, update to update a library, and remove to remove a library from compser.json. This command line-based dependency installs the package from the packagist repository.

Q25. How to get ip address of user in PHP?

PHP provides an easy way to get the user's IP address. You need to use the $_SERVER variable and REMOTE_ADDR to get the IP address of a user in PHP.

//Command to get the IP address 
Echo ‘User IP address’ .$_SERVER[‘REMOTE_ADDR’];

Q26. Explain PHP traits?

In PHP, using traits, you can reuse a method in multiple classes. Traits are just a group of methods that cannot be instantiated but is used in another class. It is like an abstract class. The trait keyword is used to declare the trait method. You can declare multiple trait methods and use them in a class too.

//syntax 
<?php 
trait traitmethodname { 
//code statement 
} 
?> 

To use a trait in a class, you need to include it with the ‘use’ keyword.

//syntax 
<? 
Class classname{ 
use traitmethodname; 
} 
?> 

Q27. What is difference between session and cookie?

Session – It is nothing but information that is stored in the server as a file. The session information in the server is associated with the cookie information. This information is accessible by all the pages that access the server. The session is created for a user when visiting the site and is closed after leaving the site.

Cookie – These are the files that contain user information that is stored on the client computer. It is mainly used for tracking purposes. The information is obtained from the server and is stored on the local machine for future use.

Q28. What is use of PHP.ini file?

PHP ini file provides information about configurations and user controls. It is read by the system to configure what a user can and cannot do with the application whenever it is initialized. Apart from this, information related to the global variables, maximum upload size, log error display, time to execute PHP script are present in this file. This file makes it easy to change these configurations. The web server needs to be restarted whenever some changes are made in this file.

Q29. What is difference between array_merge and array_combine?

Array_merge() function is used to combine two or more array elements. It is combined in such a way that the value of one array is appended to the end of the previous one. If the two arrays have the same elements then the later element overrides the previous one.

//example 
$array_1 = array(“city1”=>"delhi”, “city2”=>"mumbai”); 
$array_2 = array(“city1”=>"sydney”, “city4”=>"tokyo”); 
print(array_merge($array_1, $array_2)); 
The output will be Array([city1]=>sydney, [city2]=>mumbai, [city4]=>tokyo) 

Array_combine() function combines two arrays of equal length to create a new array. It uses one array for key and another for values. It returns false off you pass an array of different lengths.

//example 
$array_1 = array(“city1”=>"delhi”, “city2”=>"mumbai”); 
$array_2 = array(“city3”=>"sydney”, “city4”=>"tokyo”); 
print(array_merge($array_1, $array_2)); 
The output will be Array([city1]=>sydney, [city2]=>tokyo) 

Q30. Enlist few sensable functions of PHP?

AJAX (Asynchronous Javascript And XML) is a function call used for data retrieval. The call is asynchronous so the data sent and received from the server is independent. It reduces the unnecessary reload by the web page and thereby making it more interactive, versatile, robust and convenient.

CURL(Client URL) is used to get the content of the URL. It is a library used in PHP to get data from outside the current web page. It allows the code to connect with an external URL and get its response to use it in your application.

Q31. What is difference between curl and ajax?

Difference between curl and ajax

cURL (client URL) is a server-side process that is used to hit a URL from your code to get an HTML response from it. It is called before the page is rendered. It is mainly used when you are trying to retrieve the information and send it to the user immediately.

AJAX (Asynchronous JavaScript and XML) is a client-side call for exchanging data asynchronously. That is, the data is sent and retrieved to and from the server to the client independent of the main program. So, the web page doesn’t have to reload every time when new information is provided.

PHP vs ASP

PHP

ASP

It is faster

It is slower

It is easy to run

It is cumbersome to run

PHP vs Perl

PHP

Perl

It is easy and less confusing to use

It is very complicated

PHP can perform most of the Perl tasks in a simple way

Perl is very complicated and difficult for the users to use it

It is matured and evolved language.

It is a very tried and true language.

Pros of PHP

Low Barrier of Entry:

It is very easy to use PHP. Users find it very easy to make the web pages on it. Basically, PHP is designed to look after the priority of the simple building personal home pages and interpreting forms.

Functional and Object-Oriented Programming:

The current version of the PHP supports functional and object-oriented programming with anonymous functions which is also known by lambdas or closures.

Huge Ecosystem:

PHP is surrounded by the gigantic ecosystem. It is because of the popularity of WordPress and Magento. You can get online training to learn PHP which is absolutely free. There are also local tutors who teach you all about the PHP.

A Multitude of Pluggable Frameworks:

The PHP community had introduced the PHP Framework Interop Group to include interoperability. This is important because this allows porting of code from one framework to another framework. This led to having many good frameworks like Laravel, Silex and many others who share common libraries.

Automation Tools:

A great number of automation tools are available for testing and deploying PHP application that are scripted in PHP

First Class Debugging:

It is possible for the PHP to perform high-class debugging which includes remote debugging. With the emergence of the Virtual machine world of Vagrant and Docker, it becomes more and more prominent. It can be possible because of Xdebug.

Cons of PHP

Interpreted Language:

PHP is an interpreted language same as Python and Ruby that is organized down to Opcode. However, this problem is sorted to a very extent but warm up is still problematic.

Threaded Execution:

Although the arrival of the FastCGI Process Manager (FPM), it's still a reservoir of threaded executors. Due to this, many types of scalability issues have to face. It bothers all of the threaded runtimes for other languages like Python WSGI, Ruby Rack, Java Servlets or Microsoft IIS.

Every thread has its own memory space. So, you have a limited amount of storage for the number of connections that you can support. Most of the language have asynchronous I/O solution available. PHP has React and Icicle but they don't have a meaningful install base.

Global Extensions:

PHP needs extension which works as a bridge between C code and PHP to be installed globally to the PHP feasible. To access the file, they require the modification of the global configuration file. Most of the other interpreted languages give assistance for libraries to implement extensions that can be installed by the package management tool. It is possible with the help of systems administrator. It also makes getting deploying available in treated application scenarios more complex.

No IoT:

Except for Node.js, every interpreted language has their minimal IoT (Internet of Things) community but for PHP it seems like there is none. If there is one it is hiding. It is because of the shortage of well-established asynchronous I/O framework which is necessary to handle I/O well

 

Hiring PHP Developers:

As there are fewer restrictions of entry and having no concept of "Senior PHP Developer" gives a real hard problem finding good talent in PHP. It is hard to find the top-level developer whereas compared to the other languages.

Language Perception:

As already explained the barrier of entry is low and having the popular and legacy code bases such as WordPress, the world is feeling it as an insecure and entry-level language. Moreover, PHP has not acknowledged cool in over ten years. This can cause problems for both the developers and companies.

In resume, your value and marketability as a developer of PHP can be reduced if it is all filled with PHP.

In the case, if you want to sell your company with having an average level developer, it appears more financially viable to the buyer than having the team of the best PHP engineers. Therefore, if you want to raise the value of your company, then try to have a team of Node.js and java experts

With the emergence of the PHP Framework Interop Group around five years ago, the PHP community has finally embraced interoperability. This allows writing code for one framework that can be more easily ported to another. It also means you can use many of the same libraries across frameworks. Interoperability also means that core components are shared and it becomes easier to build and maintain a framework while focusing on the use case for your framework rather than the core components that every framework must provide. This has led to an abundance of really good frameworks like Symfony2, Laravel, Silex, and Zend-expressive that share common libraries.

About Author :

  • Author of PHP Interview Questions

    Sharad Jaiswal

    Sharad Jaiswal is Sr. Web Developer from Noida area. He have rich experience in PHP, Angular Js, React, javascript and Node .If you any query or project on these programming you can drop your enquiry in comment section.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.