Technical Interview

PHP Interview Questions



Several jobs require candidates to have a profound knowledge of PHP. These PHP Interview Questions have been designed specially to get you acquainted with the nature of questions that you may encounter during your interview for the subject of PHP.

1. What is PHP?

PHP stands for Hypertext Preprocessor. It is an interpreted server-side scripting language that is used for creating dynamic and interactive web page. It is also used for developing web based software applications. PHP is an open source scripting language which makes it one of the most used language in web development. It can set cookies of a website and restrict access of some web page of your website. It can also handle forms, file I/O and interact with databases.


2. What are the features of PHP?

Some important features of PHP are given below:

  • Simple & Familiar Language - PHP is a simple language language and its syntax is similar to 'C' language. PHP is very logical and well organized general-purpose programming language.
  • Fast & Efficient Language - PHP is faster than other scripting language e.g. ASP.NET, PERL and JSP. The faster speed of PHP is beneficial for users for its server administration and mail functionality.
  • Open-Source Language - PHP is an open source language which makes it freely usable and distributable, even for commercial use.
  • Platform Independent Language - PHP can be interpreted on various operating systems including UNIX-based systems, Linux, Mac OS and various versions of Windows.
  • Case-Sensitive Language - PHP is a case sensitive language. In PHP all keywords (e.g. if, else, while, for, echo, etc.), classes, functions and user-defined functions are NOT case-sensitive. User-defined variable are case-sensitive.
  • Interpreted Language - PHP is an interpreted language. There is no need of compilation of the PHP scripts.
  • Magic Constants - PHP have many built-in magic methods which starts with double underscore __, e.g. directory path of the executed file can be accessed using magic constant: __DIR__.
  • Object-Oriented Programming Language - PHP supports object oriented programming.
  • Loosely typed language - PHP supports the use of variables without declaring its data types. It is taken at the time of the execution based on the type of data the variable has.
  • Real-Time Access Monitoring - PHP provides logging accesses by creating the summary of recent accesses for the user.
  • Error Reporting - PHP have errors reporting constants to generate errors and warnings at run time. For example E_ERROR, E_WARNING, etc.

3. What is "echo" in PHP?

The echo statement is used to display output in PHP. The echo statement can be used with or without parenthesis - echo and echo().

The example below illustrates how to use echo statement for displaying text and variables.

<?php
  $x = 15;
  $y = 10;    
  echo "Single parameter is used with echo statement.\n";
  echo "Multiple ","parameters ", "are used with echo statement.\n";
  echo "Value of x is ".$x." and Value of y is ".$y.".\n";
  echo "Value of x is $x and Value of y is $y.\n";
?>

The output of the above code will be:

Single parameter is used with echo statement.
Multiple parameters are used with echo statement.
Value of x is 15 and Value of y is 10.
Value of x is 15 and Value of y is 10.

4. What is "print" in PHP?

The print statement is used to display output in PHP. The print statement can be used with or without parenthesis - print and print().

The example below illustrates how to use print statement for displaying text and variables.

<?php
  $x = 15;
  $y = 10;    
  print "Only single parameter can be used with print statement.\n";
  print "Value of x is ".$x." and Value of y is ".$y.".\n";
  print "Value of x is $x and Value of y is $y.\n";
?>

The output of the above code will be:

Only single parameter can be used with print statement.
Value of x is 15 and Value of y is 10.
Value of x is 15 and Value of y is 10.

5. What is the difference between "echo" and "print" in PHP?

echo and print statements are almost same with some minor differences that are listed below:

  • echo doesn't return any value while print returns value of 1. Therefore, print can be used in a expression.
  • echo can take multiple parameters separated by comma (,) while print takes only one argument.
  • echo is marginally faster than print.