PHP Interview Questions and Answers for Experienced

How will you find out the value of current session id?
By using: session_id()

How to connect mysqli with php using Object-Oriented Way?
$host = “localhost”;
$username = “root”;
$password = “”;
$conn = new mysqli($host, $username, $password);
//connect to server
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
echo “Connected successfully”;

How to save image in directory from URL?
In Below code, Just update the $savePath and$ downloadImagePath.Script is Ready.
try {
$savePath = $_SERVER[‘DOCUMENT_ROOT’];
$ch = curl_init($downloadImagePath);
$fp = fopen($savePath . ‘/aboutcity.png’, ‘wb’);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
echo “Image Downloaded Successfully in “.$savePath.’ Folder.’;
} catch (Exception $e) {
echo $e->getMessage();

Give Curl example using post method?
$postData = array(
“site” => “web technology experts notes”,
“dailyuser” => “1000”,
“location” => “India”
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output = curl_exec($ch);
echo $output;

How to delete a php file from server?
unlink($file); //make sure you have enough permission to do delete the file.

What is the difference between $message and $$message?
The only difference between $message and $$message is that, $message is a normal variable and $$message is variable to variable. The difference in functioning is shown below:

What Is a Persistent Cookie?
Persistent cookie is a cookie which is permanently stored on user’s computer in a cookie file. They are used for tracking the user information of the users who are browsing from a very long time. They also have the drawbacks of being unsecure, as user can see the cookies which are saved on the computer.

What are different types of Runtime Errors in PHP?
There are three types of Runtime Errors in PHP. They are as follows:-
– Notices: These are trivial, non-critical errors eg: undefined variable
– Warnings: These are serious errors eg. Including non-existing file
– Fatal errors: These are critical errors eg. Calling of non-existent function.

What is a PHP Session?
PHP session allows you to store the user session, like their information on server for faster access and for later use like username, IP addresses, their actions etc. The information which is saved is temporary and can be deleted after the user is no longer active. Example of starting a PHP session is as follows:

How can I execute a PHP script using command line?
You can execute a PHP script by running the Command line interface program, in which you can enter the PHP script file as an argument. If the file is made for the web interface then it may not execute properly using command line. Command line allows faster execution of the statements and gives faster results.
How can we encrypt the password using PHP?
crypt() function is used to create one way encryption. It takes one input string and one optional parameter. The function look is defined as: crypt (input_string, salt), where input_string consists of the string which has to be encrypted and salt is an optional parameter. PHP uses DES for encryption. The format is as follows:-

What do you mean by Persistent Cookie?
A cookie which is stored in a cookie file permanently on the browser’s computer.

Differentiate between DROP a table and TRUNCATE a table.
DROP – It will delete the table and table data.
TRUNCATE – It will delete data of the table but not the table definition.

Explain the “unlink” and “unset” functions.
unlink() function is for file system handling. It just deletes the file in context.
unset() function is for variable management. It makes a variable undefined

What is the default session time in PHP?
20 mins.

How to convert object data to array?
It will convert an object to array.
$obj = new stdClass();
$obj->key1 =’Value1′;
$obj->key2 =’Value3′;
$obj->key3 =’Value3′;
$obj->key4 =’Value4′;
$array = (Array)$obj;
print_r($array);//Array ( [key1] => Value1 [key2] => Value3 [key3] => Value3 [key4] => Value4 )

What is PHP_EOL in PHP?
PHP_EOL is used, when you want a add new line in cross-platform. It will work DOS/Mac/Unix issues.

How to store array in Constants?
You can not store array in php constants.
Because php constant can store single string only.
BUT if you serialize an array to string then you can store it constants in constnats.
See Examples:
define (“MY_FRUITS”, serialize (array (“apple”, “cherry”, “banana”,”mango”)));

Create a folder if it doesn’t already exist?
file_exists is used to check directory exist OR NOT.
mkdir is used to create the new directory.
if (!file_exists($path)) {
mkdir($path, 0755, true);

How to convert date to timestamp in PHP?
strtotime function is used to convert the full date time into timestamp.
$timestamp = strtotime(’22-09-2008′);

What is Difference between isset and array_key_exists?
isset: It is used to check, variable is defined OR NOT.
array_key_exists: it is used to check, an key is available in array or not.
bool array_key_exists ( mixed $key , array $array );

How to connect mysqli with php using PDO?
$host = “localhost”;
$username = “root”;
$password = “”;
$conn = new PDO(“mysql:host=$host;dbname=myDB”, $username, $password);
// set the PDO error mode to exception
echo “Connected successfully”;

Difference between array_merge and array_combine in php?
array_merge example
$array1 = array(‘one’,’two’);
$array2 = array(1,2);
$result = array_merge($array1,$array2);

array_combine example
$array1 = array(‘one’,’two’);
$array2 = array(1,2);
$result = array_combine($array1,$array2);

How to convert array to json in php?
$array = array(‘one’,’two’);
echo json_encode($array); //use json_decode for decode

How to serialize an array in php?
$array = array(‘one’,’two’);
echo serialize($array);//use unserialize for convert serialized string to array

How to call constructor of parent class?

How to return JSON from a PHP Script?
$array = array(
header(‘Content-Type: application/json’);
echo json_encode($array);

Leave a Reply

Your e-mail address will not be published. Required fields are marked *