Thursday, 16 June 2016

PHP interview questions and answers for experienced candidates

PHP interview questions and answers for experienced candidates

Question: How to POST Data using CURL in PHP?
$url = "http://www.example.com/ajax/testurl";
$postData = 'this is raw data';
try {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postData);
    curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain')); 
    echo $output = curl_exec($ch);
    curl_close($ch);
} catch (Exception $e) {
    echo $e->getMessage();die;
}



Question: How to re-index an array?
$arrayData=array(
    1=>'web technology experts',
    3=>'web technology experts Notes',
    4=>'web technology experts Notes  php technology',    
    10=>'PHP interview questions and answers'    
);

$reIndexArray = array_values($arrayData);
print_r($reIndexArray);
/*
 Array ( [0] => web technology experts [1] => web technology experts Notes [2] => web technology experts Notes php technology [3] => PHP interview questions and answers ) 
 */



Question: How do I add 24 hours to a in php?
$currentDate ='2016-06-16 14:30:10';
$futureDate=strtotime('+1 day', strtotime($currentDate));
echo date('Y-m-d H:i:s',$futureDate); //2016-06-17 14:30:10



Question: How to declare a global variable and get them?
//Set Global value
$GLOBALS['name'] = 'Web technology experts notes';

//Get Global value
echo $GLOBALS['name'];



Question: How to get the location of php.ini?
phpinfo();

This is let you know where is your php.ini file located.


Question: How to Serializing PHP object to JSON?
$obj=  new stdClass();
$arrayData=array(
    1=>'web technology experts',
    3=>'web technology experts Notes',
    4=>'web technology experts Notes  php technology',    
    10=>'PHP interview questions and answers'    
);  
$obj->data =$arrayData;

echo json_encode($obj);

Output
{"data":{"1":"web technology experts","3":"web technology experts Notes","4":"web technology experts Notes php technology","10":"PHP interview questions and answers"}}



Question: How to get duplicate values from array?
$array = array('apple', 'orange', 'pear', 'banana', 'apple',
'pear', 'banana', 'banana', 'orange');
print_r(array_count_values($array));

Output
Array
(
    [apple] => 2
    [orange] => 2
    [pear] => 2
    [banana] => 3
)


Question: How to identify server IP address in PHP?
$_SERVER['SERVER_PORT']; 



Question: How to generate PDF File in PHP? You can use third party library.
http://www.fpdf.org/