PHP Interview Question Answers List:


Q: What is difference between Single and Double Quote Strings:

When you write a string in single quotes it will take string literally. Suppose you have a variable $num=10; and if you write something like this ‘Value of num=$num’. In this case, it will show exactly the same. But if you will write this in double-quotes the variable name will be changed with its value.

Q: What is cookie in PHP?

In PHP Cookies are used to store some data on client’s machines in the form of a text file. The main motive of storing cookies on the client machine is to identify the user when the user will revisit a site. Because whenever a user requests a web page to the server it also sends cookies to the server.

Q: What is global in PHP?

The global variables are also known as superglobals and These these superglobal variables first time introduced in PHP 4.1.0 on 10 December 2001. A very interesting thing about these superglobal variables is these variables are always accessible. You can access them in different PHP files easily.

Q: List global variables in PHP?

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION

Q: How to create a new file using PHP?

We use fopen() function to create a new file in PHP File Handling. The fopen() function works in a very simple manner if a file already exists it will open that otherwise, it will create a new file. It is important to note if you want to create a new file then the file must file is opened for writing (w) or appending (a).

<?php
$newfile = fopen("NewFile.txt", "w") or die("Couldn't open this file");
fclose($newfile);
?>

 

Q: What are operators in PHP

Operators are special symbols that tell to PHP processor to do some special operations. For example, There is a + operator which tells PHP Processor to add two numbers (4+5). There are various operators available in PHP and they are divided into some categories. List of Categories:

  • Arithmetic Operators
  • Comparison Operators
  • Assignment Operators
  • Inc/Dec Operators
  • Logical Operators
  • String Operators
  • Array Operators

Q: What is class and objecet in PHP?

In very easy language a class is the blueprint of the object and an object is a run time instance of that class. In PHP, the class keyword is used to define a class. We write the class keyword followed by the class name and after that, we define the properties and methods of the class in curly braces {}.

An object is a run time instance of the class. We can create multiple objects of any class and each object will have all the properties and methods defined in the class. Each object will occupy a different space in memory and each object have the ability to set different values for properties.

Q: What is Constructor in PHP?

The constructor is a special member function in class and the main motive to create the constructor in class is to initialise the properties of every object instance of the class. An amazing thing about the constructor is, the constructor is automatically called on each newly-created object.

Here you can check an example program to understand how to create a constructor in PHP:

<?php
class Example{

  //defining constructor
  function __construct() {
    echo "This is Constructor in Example Class";
  }
 
}

$obj= new Example();

?>

Q: What is destructor in PHP?

The destructor method is totally reverse of the constructor method. PHP destructor does not return any value. In case we create __destructor function in class, PHP will automatically call the destructor function if there are no other references to a particular object or at the end of the script.

Here an example program to show how to define destructor in PHP.

<?php
class Example {

  //defing a constructor
  function __construct() {
    echo "This is constuctor method.<br>";
  }
  
  //defing a normal method
  function show(){
     echo "This is show method.<br>";
  }
  
  //defing a destructor
  function __destruct() {
    echo "The is destructor method.<br>"; 
  }
}

$obj = new Example();
$obj->show();
$obj->show();
$obj->show();
?>
 

The destructor will be called even if script execution is stopped using exit() or die() function.

Q: What are Access Modifiers in PHP?

Access Modifiers are used to control the accessibility of properties and methods. For example, you want to create a method that you do not want to make accessible from outside of the class. You can easily do this with the help of Access Modifiers.

There are three types of Access Modifiers in PHP, which are as follow. 

  • public
  • protected
  • private