Top 50 PHP interview questions and answers in 2020 for freshers and experience
List of Top 50 PHP interview questions and answers :
Q#1) What is the difference between include() and require() ?
Answer:
include() and require() function both are used to include a php script from one file to another file.
But there is some difference between them.
If there is any error occured while including file using include() function then it generate php warning but
execution of script continue.
While in case of require() function it generate fatal error and stop the execution of script.
Example 1:
<?php include('abc.php'); echo "print message1 here"; ?>
Output:
Warning: include(abc.php): failed to open stream: No such file or directory in E:\xampp\htdocs\test\index.php on line 3
Warning: include(): Failed opening ‘abc.php’ for inclusion (include_path=’E:\xampp\php\PEAR’) in E:\xampp\htdocs\test\index.php on line 3
print message1 here
Example 2
<?php require('abcd.php'); echo "print message2 here"; ?>
Output :
Warning: require(abcd.php): failed to open stream: No such file or directory in E:\xampp\htdocs\test\index.php on line 4
Fatal error: require(): Failed opening required ‘abcd.php’ (include_path=’E:\xampp\php\PEAR’) in E:\xampp\htdocs\test\index.php on line 4
Q#2) How to enable error reporting in php ?
Answer:
First we have to check ” display_error ” is ON in php.ini file or we can declare ” ini_set(‘display_errors’,1) ” in our script. After this we have to include “error_reporting(E_ALL) ” in our code to display different kind of error messages during the script execution.
To find out the exact cause of error we have to enable error message in our code for debugging purpose.
Q#3) In PHP, is 123 == 0123 ?
Answer:
If we prefix a number with 0 indicate octal (base 8 ) and 0x indicate hex (base 16 )
Code:
var_dump(123);
var_dump(0123);
Output :
int 123
int 83
This is because 0123 is octal notation while 123 is decimal.
Therefore 123 == 0123 is false . 123 is not equal to 83.
Q#4) How can I get current session Id in php ?
Answer:
The function session_id() is used to get or set the session id for the current session.
session_start();
echo session_id();
Q#5) How to send post request without using html from php file ?
Answer:
We can send post request without using html in php with the help of CURL that allow us to connect and communicate with different types of servers and different types of protocols like http, https, ftp etc.
Q#6 ) What is Magic method in PHP ?
Answer :
In php methods or functions that start with double underscore ( __ ) are called magic method. These
methods or functions are called automatically when some conditions are met. These functions are always defined
inside classes and must be declared public.
For example :
<?php class Student { private $rollno; private $marks; public function __construct($rollno;$marks) { $this->name = $rollno; $this->email = $marks; } } $objStudent = new Student('5', 98); ?>
List of some magic mathods are :
__construct();
__destruct();
__call();
__get();
__set();
__isset();
__toString();
__unset();
__autoload();
__clone();
Q#7 ) Final defined class can be extended ?
Answer.
No, we can not extend a Final defined class. When a class is defined with Final keyword that class or method prevents child class or method overriding.
Q#8 ) What is difference between single-quoted and double-quoted strings in php ?
Answer :
Single-quoted Strings display the text as it is .
Double quoted strings display escaped character ( including some regexes ), and variable in the strings will be evaluated.
Example :
$money = " Us dollars"; $place = "India"; echo ' Basu lives in $place and earns in $money.'; // Basu lives in $place and earns in $money. echo " Basu lives in $place and earns in $money."; // Basu lives in India and earns in Us dollars.
Q#9 ) How to increase maximum execution time in PHP ?
Answer :
We can use at the top of our PHP script
ini_set('max_execution_time', '500'); // 500 seconds ini_set('max_execution_time', '0'); // infinite time of execution
We can also use :
set_time_limit ( int $seconds );
But this function will not work if PHP is running in safe mode. You have to off safe mode or have to change limit in php.ini file.