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);
error_reporting(-1);

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

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

Question: How to POST request with PHP?
$ch = curl_init();
$postData='var1=value1&var2=value2&var3=value3';
curl_setopt($ch, CURLOPT_URL, "http://mydomain.com/ajaxurl");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($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) {
    session_start();
}


Question: Why do we use ob_start()?
define ("FRUITS", serialize (array ("apple", "mongo", "banana","fig")));
$fruits = unserialize (FRUITS);
print_r($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");
print_r($array);
$array['bbbbb']= $array['b'];
unset($array['b']);
print_r($array);



Question: How to get last key in an array?
$array= array ('a'=>"apple", 'm'=>"mongo", 'b'=>"banana",'f'=>"fig");
end($array);
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;
            $data.="\n";
        }
        fclose($stream);

        echo $data;
    }



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

output
21, 21