PHP Interview Questions
1 .
What is the difference between unlink() and unset()?
The unlink() function is used when you want to delete the files completely. The unset() Function is used when you want to make that file empty.
 
Unlink() function : The unlink() function is an inbuilt function in PHP which is used to delete a file. The filename of the file which has to be deleted is sent as a parameter and the function returns True on success and False on failure. The unlink() function in PHP accepts two-parameter.

Syntax : unlink(‘path to file’);
 
Unset() function : The Unset() function is an inbuilt function in PHP which is used to remove the content from the file by emptying it. It means that the function clears the content of a file rather deleting it. The unset() function not only clears the file content but also is used to unset a variable, thereby making it empty.

Syntax : unset($var);
2 .
How will you calculate days between two dates in PHP?
Calculating days between two dates in PHP
 
<?Php 
$date1 = date('Y-m-d');
$date2 = '2017-11-21';
$days = (strtotime($date1)-strtotime($date2))/(60*60*24);
echo $days;
?>

Output : 1063
3 .
What is Mcrypt used for?
MCrypt is a file encryption function and that is delivered as Perl extension. It is the replacement of the old crypt() package and crypt(1) command of Unix. It allows developers to encrypt files or data streams without making severe changes to their code.
 
MCrypt is was deprecated in PHP 7.1 and completely removed in PHP 7.2.
4 .
What is Pear in PHP?
PEAR stand for PHP Extension and Application Repository. PEAR provides :
 
* A structured library of code
* maintain a system for distributing code and for managing code packages
* promote a standard coding style
* provide reusable components.
5 .
How to get the IP address of the client/user in PHP?
You can use $_SERVER['REMOTE_ADDR'] to get IP address of user/client in PHP, But sometime it may not return the true IP address of the client at all time. Use Below code to get true IP address of user.
 
function getTrueIpAddr(){
 if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
  {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  }
    else
  {
      $ip=$_SERVER['REMOTE_ADDR'];
  }
    return $ip;
}
6 .
Explain Traits in PHP?
Traits in PHP are similar to Abstract classes that are not be instantiated on its own. Traits allow us to declare methods that are used by multiple classes in PHP. trait keyword is used to create Traits in PHP and can have concrete and abstract methods.
 
Syntax : 
<?php
  trait TraitName {
  // some code...
}
?>
7 .
Does PHP support multiple inheritances?
PHP does not support multiple inheritances. To implement the features of multiple inheritances, the interface is used in PHP.
 
Example :
 
Here, two interfaces, Isbn and Type are declared and implemented in a class, book details to add the feature of multiple inheritances in PHP.
 
interface Isbn { 
public function setISBN($isbn);
}
interface Type{
public function setType($type); 
}
class bookDetails implements Isbn, Type {
private $isbn; 
private $type; 
public function setISBN($isbn)
{ 
$this -> isbn = $isbn; 
}
public function setType($type)
{ 
$this -> type = $type; 
}
}
8 .
What is garbage collection?
It is an automated feature of PHP.
 
When it runs, it removes all session data which are not accessed for a long time. It runs on /tmp directory which is the default session directory.
 
PHP directives which are used for garbage collection include :
 
* session.gc_maxlifetime (default value, 1440)
* session.gc_probability (default value, 1)
* session.gc_divisor (default value, 100)
9 .
What is URL rewriting?
Appending the session ID in every local URL of the requested page for keeping the session information is called URL rewriting.
 
The disadvantages of these methods are, it doesn’t allow persistence between the sessions and, the user can easily copy and paste the URL and send it to another user.
10 .
What is PDO?
The full form of PDO is PHP Data Objects.
 
It is a lightweight PHP extension that uses a consistence interface for accessing the database. Using PDO, a developer can easily switch from one database server to the other. But it does not support all the advanced features of the new MySQL server.