Thursday 17 August 2017

PHP Technical Interview Questions and Answer for 2 year experienced

PHP Technical Interview Questions and Answer for 2 year experienced

Question: What is the use of the @ symbol in PHP?
It suppresses error messages, means hide the error/warning messages for that statement.
echo $noDefinedVar;

Question: How to enable all error in PHP?
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);

Question: How to disable all error in PHP?
ini_set('display_startup_errors', 0);
ini_set('display_errors', 0);

Question: How to convert a variable to string?
$myText = (string)$myVar; // Casts to string

Question: How to POST request with PHP?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

echo $result;die;

Question: Formatting a number with leading zeros in PHP?
echo sprintf('%08d', 1234567); //01234567
echo sprintf('%09d', 1234567); //001234567
echo sprintf('%010d', 1234567); //0001234567

Question: How to Check if PHP session has already started?
if (session_status() == PHP_SESSION_NONE) {

Question: Why do we use ob_start()?
define ("FRUITS", serialize (array ("apple", "mongo", "banana","fig")));
$fruits = unserialize (FRUITS);

Question: How to increase PHP Execution Time?
ini_set('max_execution_time', 300);

Question: how do you change the key of an array element?
$array= array ('a'=>"apple", 'm'=>"mongo", 'b'=>"banana",'f'=>"fig");
$array['bbbbb']= $array['b'];

Question: How to get last key in an array?
$array= array ('a'=>"apple", 'm'=>"mongo", 'b'=>"banana",'f'=>"fig");
echo key($array);//k

Question: Set HTTP header to UTF-8 using PHP?
header('Content-Type: text/html; charset=utf-8');

Question: How to generate a random, unique, alphanumeric string?
echo md5(uniqid(rand(), true));

Question: How to run SSH Commands from PHP?
$connection = ssh2_connect('HOST_OR_IP_ADDRESS', 'PORT');
ssh2_auth_password($connection, 'USERNAME', 'PASSWORD');

$stream = ssh2_exec($connection, 'ls');
if ($stream) {
        echo "fail: unable to execute command\n";
    } else {
        // collect returning data from command
        stream_set_blocking($stream, true);
        $data = "";
        while ($buf = fread($stream,4096)) {
            $data .= $buf;

        echo $data;

Question: What is output of below?
$a = '1';
$b = &$a;
$b = "2$b";
echo $a.", ".$b;

21, 21