This is very important list of 21 Best PHP Programming interview questions that has been asked by interviewer in most of interview from fresher and experienced PHP programmer to find output from given below code.
What is PHP $ and $$ Variables ?
Ans – $ (single dollar) – It is just a like normal variable that stores the value like string, integer, float, etc.
$$ ( double dollar ) – It is a reference variable that store the value inside $var
<?php $x = "php"; $$x = 500; echo $x; echo $$x; echo $php; ?>
Output –
php
500
500
What is echo and print in PHP ?
Ans – There are two basic way ECHO and PRINT to get output in PHP. The main difference between echo and print is echo returns value the given and print returns true and false after execute the code.
In echo where we can pass multiple parameters and its represents as echo keyword
we can called print by PRINT() keyword.
What is Constructor and Destructor in PHP OOPS ?
Ans – It is special types of function in php oops , That is called automatically when object is created from class.
// In PHP4 version class Class{ function Class(){ // constructor logic here } }
//In PHP5+ Version class Class{ function __construct(){ // constructor logic here }
__construct( ) : It is magic methods that start with double underscore . It override the PHP default action .
Destructor: It is special type of function that is invoked automatically when object of class is deleted or out of scope.
class Class{ function __construct(){ // logics in constructor } function __destruct(){ // this is destructor } }
There are some types of constructor :
Default constructor
It is constructor that is called without having parameter.
Parameterized constructor
It is constructor that having atleast one parameters.
class Animal{ public $name; public $colour; public function __construct($name){ // parameterized constructor $this->name = $name; } } $carObj = new Animal("maruti suzuki"); // parameterized constructor echo $carObj ->name; //Output: maruti suzuki
Copy Constructor –
class Animal{ public $name; public $model; public function __construct($name){ // parameterized constructor $this->name = $name; } public function __clone(){ } } $carObj1 = new Animal("maruti suzuki"); // parameterized constructor $carObj2 = clone $carObj1 ; echo $carObj2 ->name; //Output: maruti suzuki
Static Constructor
Private Constructor : It makes ensure that only one instance of class can created . It is example of singleton class that provides the global access of that instance –
class Class { private static $instance; private function __construct() { } public static function get_instance() { if (!self::$instance) self::$instance = new Class(); return self::$instance; } }
What will be output for below code ?
$x = true and false;
var_dump($x);
Output – bool(true)
What will be output for below code ?
$z = 5;
echo $z;
echo "<br />";
echo $z+++$z++;
echo "<br />";
echo $z;
echo "<br />";
echo $z---$z--;
echo "<br />";
echo $z;
Output –
5
11
7
1
5
What is value of $a and $b after below code is executed ?
$a = '1';
$b = &$a;
$b = "2$b";
Output –
$a = 21
$b = 21
What is output of each statement below after execution code ?
var_dump(0123 == 123);
var_dump('0123' == 123);
var_dump('0123' === 123);
OutPut -
bool(false)
bool(true)
bool(false)
What will be output of given below code ?
$text = ' alexa ';
$text[10] = 'Doe';
echo $text ;
Output -
alexa