Friday, 20 May 2016

PHP Interview Questions for 3 year Experience

PHP Interview Questions for 3 year Experience

Question: How to explode a string using multiple delimiters("," and "|" )?
$string='php, interview | questions , and | answers ';
$output = preg_split( "/(\,|\|)/", $string );
print_r($output );
/*
Array ( [0] => php [1] => interview [2] => questions [3] => and [4] => answers ) 
*/



Question: What is Late static bindings in PHP?
Late static bindings comes in PHP 5.3.0. It is used to reference the called class in the context of static inheritance.


Question: Why cause $_FILES may be empty when uploading files to PHP?

Following reason may be for empty of $_FILES

  1. multipart/form-data is missing in form tag. <form action="vanilla-upload.php" enctype="multipart/form-data" method="POST"> </form>
  2. /tmp folder does not have enough permission. In this folder uploaded file moved temporary.
  3. You have upload heavy files more than allowd in php.ini (post_max_size and upload_max_filesize)
  4. file_uploads is off in php.ini



Question: How to convert a UNIX Timestamp to Formatted Date?
$timestamp='1463721834';
echo date("Y-m-d h:i:s A", $timestamp);//2016-05-20 05:23:54 AM



Question: How can we remove a key and value from an associative array?
$arrayData = array(0=>'one',1=>'two',3=>'three'); 
print_r($arrayData);
unset($arrayData[1]);
/*
Array ( [0] => one [3] => three ) 
 */



Question: How can we remove a value from an associative array?
$arrayData = array(0=>'one',1=>'two',3=>'three'); 
$arrayData[1]='';
print_r($arrayData);
/*
Array ( [0] => one [1] =>  [3] => three ) 
 */



Question: What is meaning of ampersand(&) before the function name?
If any function start with ampersand(&), It means its call by Reference function. It will return a reference to a variable instead of the value.


Question: How to detect request is from Search Engine Bots?
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot"))
{
    /*This request is from search engine bots */
}



Question: List all files/directory in one directory?
if ($handle = opendir('D:\softwares')) {

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != "..") {

            echo "$entry";
            
        }
    }

    closedir($handle);
}



Question: What is mod_php?
mod_php means PHP installed as a module in Apache.


Question: How to Add 1 Day in timestamp?
$timestamp='1463721834';//2016-05-20 05:23:54 AM
//Add 1 Day
$timestamp=strtotime('+1 day', $timestamp);
echo date("Y-m-d h:i:s A", $timestamp);//2016-05-21 05:23:54 AM



Question: How to re-index an array?
$arrayData = array(0=>'one',1=>'two',3=>'three',66=>'six six',7=>'Seven'); 

$newArray = array_values($arrayData);
print_r($arrayData);
/*
 * Array ( [0] => one [1] => two [3] => three [4] => six six [5] => Seven ) 
 */


Question: How to restrict the function to be accept only array?
We can define the argument data-type, while defining the function. For Example
function testmsg(array $data) {
    print_r($data);
}

testmsg('string');

It will throw following error.
Catchable fatal error: Argument 1 passed to testmsg() must be of the type array, none given