PHP Interview Questions
1 .
What are constructor and destructor in PHP ?
PHP constructor and destructor are special type functions that are automatically called when a PHP class object is created and destroyed.
 
Generally, Constructor is used to initializing the private variables for class and Destructors to free the resources created /used by the class.
 
Here is a sample class with a constructor and destructor in PHP.
 
<?php
class Sample {
   
    private $name;
    private $link;

    public function __construct($name) {
        $this->name = $name;
    }

    public function setLink(Sample $link){
        $this->link = $link;
    }

    public function __destruct() {
        echo 'Destroying: '. $this->name;
    }
}
?>
2 .
What is purpose of @ in Php?
In PHP @ is used to suppress error messages.When we add @ before any statement in php then if any runtime error will occur on that line, then the error handled by PHP
3 .
What is default session time and path in PHP. How to change it?
The default session time in PHP is 1440 seconds (24 minutes) and the Default session storage path is temporary folder/tmp on the server.
 
You can change default session time by using below code
 
<?php
 // server should keep session data 
    for AT LEAST 1 hour
    ini_set('session.gc_maxlifetime', 3600);

 // each client should remember their 
    session id for EXACTLY 1 hour
    session_set_cookie_params(3600);
?>
4 .
What is namespaces in PHP?
PHP Namespaces provide a way of grouping related classes, interfaces, functions and constants.

# define namespace and class in namespace
  namespace Modules\Admin\;
  class CityController {
}
# include the class using namesapce
  use Modules\Admin\CityController ;‚Äč
5 .
What are PHP Magic Methods/Functions. List them.
In PHP all functions starting with __ names are magical functions/methods. Magical methods always lives in a PHP class.The definition of magical function are defined by programmer itself.
 
Here are list of magic functions available in PHP
 
__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo().
6 .
Explain Type hinting in PHP?
In PHP Type hinting is used to specify the excepted data type of functions argument. Type hinting is introduced in PHP 5.
 
Example usage :
 
//send Email function argument $email Type hinted of Email Class. It means to call this function you must have to pass an email object otherwise an error is generated.
 
<?php
function sendEmail (Email $email)
{
  $email->send();
}
?>
7 .
What are different types of errors available in Php ?
There are 13 types of errors in PHP, We have listed all below
 
E_ERROR: A fatal error that causes script termination.
E_WARNING: Run-time warning that does not cause script termination.
E_PARSE: Compile time parse error.
E_NOTICE: Run time notice caused due to error in code.
E_CORE_ERROR: Fatal errors that occur during PHP initial startup.
(installation)
E_CORE_WARNING: Warnings that occur during PHP initial startup.
E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.
E_USER_ERROR: User-generated error message.
E_USER_WARNING: User-generated warning message.
E_USER_NOTICE: User-generated notice message.
E_STRICT: Run-time notices.
E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error
E_ALL: Catches all errors and warnings.
8 .
How to get length of an array in PHP?
PHP count function is used to get the length or numbers of elements in an array
 
<?php
   // initializing an array in PHP
      $array=['a','b','c'];
  // Outputs 3 
      echo count($array);
?>
9 .
How to Pass JSON Data in a URL using CURL in PHP?
Code to post JSON Data in a URL using CURL in PHP
 
$url='https://www.freetimelearning.com/get_details';
$jsonData='{"name":"phpScots",
"email":"phpscots@freetimelearning.com"
,'age':28
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_close($ch);
10 .
How to get no of arguments passed to a PHP Function?
func_get_args() function is used to get number of arguments passed in a PHP function.
 
Example :
function foo() {
   return  func_get_args();
}
echo foo(1,5,7,3);//output 4;
echo foo(a,b);//output 2;
echo foo();//output 0;