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



Friday, 11 August 2017

PHP Technical Interview Questions and Answer for 3 year experienced

PHP Technical Interview Questions and Answer for 3 year experienced

Question: What is output of below script?
var_dump(0123 == 123);
var_dump('0123' == 123);
var_dump('0123' === 123);

Output
var_dump(0123 == 123);//false
var_dump('0123' == 123); //true
var_dump('0123' === 123);//false



Question: What is output of below script?
var_dump(true and false);
var_dump(true && false);
var_dump(true || false);

Output
var_dump(true and false); //false
var_dump(true && false); //false
var_dump(true || false); //true



Question: What is output of below script?
$a1=true and false;
$a2=true && false;
$a3=true || false;

var_dump($a1);
var_dump($a2);
var_dump($a3);

Output
$a1=true and false;
$a2=true && false;
$a3=true || false;

var_dump($a1);//true
var_dump($a2);//false
var_dump($a3);//true



Question: What is output of below script?
echo  10 + "10%" + "$10";

Output
echo  10 + "10%" + "$10"; //20

Reason: First 10 is 10
Second 10 is 10
Third 10 is 0 (Because start with $)


Question: What is output of below script?
$text = 'HELLO';
$text[1] = '$';
echo $text;

Output
$text = 'HELLO';
$text[1] = '$';
echo $text;//H$LLO 


Question: What is output of below script?
$x = NULL;
var_dump($x);

Output
$x = NULL;
var_dump($x);/null


Question: Why do we use ob_start()?
Ob_start used to active the output buffering .


Question: What is a .htacces file?
.htaccess is a configuration file running on server.
Following task can be done with .htacces?
  1. URL Rewrite
  2. Website password protected.
  3. Restrict ip addresses
  4. PHP/Apache Configuration
  5. Set browser caching for Speed up application.


Question: What is the difference between compiler language and interpreted language?
Interpreted language executes line by line, if there is some error on a line it stops the execution of script. Compiler language can execute the whole script at a time and gives all the errors at a time. It does not stops the execution of script ,if there is some error on some line.


Question: What is type hinting?
Type hinting means the function specifies what type of parameters must be and if it is not of the correct type an error will occur.



Question: What will be output of following?
$x = 5;
echo $x;
echo "
"; echo $x+++$x++; echo "
"; echo $x; echo "
"; echo $x---$x--; echo "
"; echo $x;



Output
5
11
7
1
5


Question: What will be output of following?
$a = 1;
$b = 2;
echo $a,$b;


Output
12


Question: What will be output of following?
var_dump(0123 == 123);
var_dump('0123' == 123);
var_dump('0123' === 123);


Output
boolean false
boolean true
boolean false