Friday 13 November 2015

PHP Difficult Interview Questions and Answers

PHP Difficult Interview Questions and Answers


Question: How to check a variable is NULL OR empty string?
var_dump($variableName);

Also for compare you can use === to compare data type.


Question: How can I execute an anonymous function?
call_user_func(function() { echo 'anonymous function called.'; });



Question: How can I measure the speed of code written in PHP?
$startTime= microtime(true);
/** Write here you code to check **/
/** Write here you code to check **/
$endTime= microtime(true);
echo 'Time Taken to executre the code:'.$endTime-$startTime;



Question: Which PHP Extension help to debug the code?
Xdebug


Question: How to get Yesterday date?
date("F j, Y", strtotime( '-1 days' ) ); //November 11, 2015



Question: How to set HTTP header to UTF-8 using?
Add following at the top of page.
header('Content-Type: text/html; charset=utf-8');



Question: How to get first 20 characters from string?
echo substr('Web Technology Experts Notes',0,20);



Question: How to get last date of month of given date?
$date = "2015-11-23";
echo date("Y-m-t", strtotime($date));



Question: How do I check with PHP if file exists?
$filename = "/folder/filename.php";
if (file_exists($filename)) {    
    echo "The file $filename exists.";    
} else {
    echo "The file $filename does not exists.";
}



Question: How to remove non-alphanumeric characters ?
echo preg_replace("/[^A-Za-z0-9 ]/", '', 'web technology experts notes @2015 '); //web technology experts notes 2015



Question: How do I replace all line breaks (\n\r) in a string with
tags?

echo str_replace(array('\r','\n','\r\n'),'
','Hello \n Web technology');



Question: How do I format a number to a dollar amount in PHP? Give Example?
Use money_format
echo money_format('$%i', 6.6); // echos '$3.40' //echos $6.60



Question: How to prevent Browser cache for php site?
Add following code at the top of web page.
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");



Question: How do i get the current script file name?
echo __FILE__; //htdocs/project/index.php



Question: How to add key/value in existing array?
$arrayname['keyname'] = 'new value';