PHP Interview Questions and Answers

PHP originally derived from Personal Home Page Tools, now stands for Hypertext Preprocessor. It is a open source server side scripting language designed for web development. It is fast, flexible and pragmatic programming language used for most popular websites.


1) What is the difference between == and === operator in PHP ?

In PHP == is equal operator and returns TRUE if $a is equal to $b after type juggling and === is Identical operator and return TRUE if $a is equal to $b, and they are of the same data type.


2) How to increase the execution time of a PHP script ?

The default max execution time for PHP scripts is set to 30 seconds. If a php script runs longer than 30 seconds then PHP stops the script and reports an error. You can increase the execution time by changing max_execution_time directive in your php.ini file or calling ini_set(‘max_execution_time’, 300); //300 seconds = 5 minutes function at the top of your php script.


3) What is the difference between "echo" and "print" in PHP?

Echo can output one or more string but print can only output one string and always returns 1 .

Echo is faster than print because it does not return any value.


4) What is the difference between $message and $$message?

$message stores variable data while $$message is used to store variable of variables.

$message stores fixed data whereas the data stored in $$message may be changed dynamically.

Example:

  1. $x = "abc";  
  2. $$x = 200;  
  3. echo $x."
    ";  
  4. echo $$x."
    ";  
  5. echo $abc;  
  6. ?>  

Ouptput:  abc

                 200

                  200


5) Explain the difference between PHP4 and PHP5.

PHP4 doesn't support oops concept and uses Zend Engine 1.

PHP5 supports oops concept and uses Zend Engine 2.


6) What are the ways to define a constant in PHP?

PHP constants are name or identifier that can't be changed during execution of the script. PHP constants are defined in two ways:

  1. Using define() function
  2. Using const() function


7) What is the use of count() function in PHP?

The PHP count() function is used to count total elements in the array.


8) what are different types of errors in php?

Basically there are four kinds of errors in PHP, which are as follows:

  1. Parse Error (Syntax Error)
  2. Fatal Error
  3. Warning Error
  4. Notice Error
  • Parse Error (Syntax Error): The parse error occurs if there is a syntax mistake in the script; the output is Parse error. A parse error stops the execution of the script. There are many reasons for the occurrence of parse errors in PHP. The common reasons for parse errors are as Unclosed quotes,Missing or Extra parentheses,Unclosed braces,Missing semicolon.

Example:


echo "Cat";
echo "Dog"
echo "Lion";
?>

Output:

In the above code we missed the semicolon in the second line. When that happens there will be a parse or syntax error which stops execution of the script.

  • Fatal Error: Fatal errors are caused when PHP understands what you've written, however what you're asking it to do can't be done. Fatal errors stop the execution of the script. If you are trying to access the undefined functions, then the output is a fatal error.

Example:

<?php
function fun1()
{
echo "Vineet Saini";
}
fun2();
echo "Fatal Error !!";
?>

Output:

In the above code we defined a function fun1 but we call another function fun2 i.e. func2 is not defined. So a fatal error will be produced that stops the execution of the script. 

  • Warning Error: Warning errors will not stop execution of the script. The main reason for warning error is to include a missing file or using the incorrect number of parameters in a function.

Example:

<?php 
echo "Warning Error!!";
include ("Welcome.php");
?>

Output:

In the above code we include a welcome.php file, however the welcome.php file does not exist in the directory. So there will be a warning error produced but that does not stop the execution of the script i.e. you will see a message Warning Error!!

  • Notice Error: Notice error is the same as a warning error i.e. in the notice error, execution of the script does not stop. Notice that the error occurs when you try to access the undefined variable, then produce a notice error.

Example:

<?php
$a="Vineet kumar saini";
echo "Notice Error !!";
echo $b;
?>

Output:

In the above code we defined a variable which named $a. But we call another variable i.e. $b, which is not defined. So there will be a notice error produced but execution of the script does not stop, you will see a message Notice Error!!


9) How can we get the IP address of the client?

By using PHP' built-in method $_SERVER["REMOTE_ADDR"]; we an get the IP address of the client.


10) What’s the difference between unset() and unlink()?

unset() sets a variable to “undefined” while unlink() deletes a file we pass to it from the file system.