Saturday, 14 November 2015

PHP Questions and Answers for experienced

PHP Questions and Answers for experienced



Question: Difference between array_map, array_walk and array_filter?
array_map: This function applies the callback to the elements of the given arrays.
$arrayData= array_map('strtolower', array('One','Two','three'));
print_r($arrayData); //array ( [0] => one [1] => two [2] => three )

array_walk: This function apply a user supplied function to every member of an array
function my_function(&$item, $key){
  $item=$item.($key+1);
}
$arrayData=array('One','Two','three');
array_walk($arrayData, 'my_function');
print_r($arrayData); //Array ( [0] => One1 [1] => Two2 [2] => three3 )

array_filter: This function filters elements of an array using a callback function.
function odd_number($item){
  if($item%2==0){
    return false;
    }else{
    return true;
    }
}
$arrayData=array(1,2,3,4,5,6);
$arrayData=array_filter($arrayData, 'odd_number');
print_r($arrayData); //Array ( [0] => 1 [2] => 3 [4] => 5 )



Question: How to convert date to timestamp in PHP??
echo strtotime('2015-09-15'); //1442275200



Question: Give an easy way to encrypt/decrypt the password?
define('SALT', 'X4DSF464ADS6SC5C5D55D5'); 
function encrypt_string($text) 
{ 
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)))); 
} 

function decrypt_string($text) 
{ 
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SALT, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))); 
} 

echo $msg = encrypt_string("your message"); //q2s33vKz5sNPPQFzZygm4nqtjuPljaI+9q1mtcwRZ3U=
echo decrypt_string($msg);//your message


Question: How to get the last character of a string in PHP?
echo substr("web tehnology", -1); // y



Question: How to store array in constants??
$arrayData=array(1,2,3,4,5,6);
define ("FRUITS", serialize($arrayData));
print_r(unserialize (FRUITS));



Question: What is the difference between bindParam and bindValue?
bindParam the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called. PDOStatement::bindValue() is not work in this way.


Question: What is use of PHP_EOL?
PHP_EOL is used to find the newline character in a cross-platform-compatible way so it handles DOS/Mac/Unix issues.


Question: How to get accurate Ip Address of client Machine?
function ip_address(){
    foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
        if (array_key_exists($key, $_SERVER) === true){
            foreach (explode(',', $_SERVER[$key]) as $ip){
                $ip = trim($ip); // just to be safe

                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
                    return $ip;
                }
            }
        }
    }
}
echo ip_address();//101.60.216.68 



Question: How to get extension of file??
$path ='/web-tech/myimage.png';
print_r(pathinfo($path, PATHINFO_EXTENSION));



Question: How to Check if PHP session has already started?
if(session_id() == '') {
    echo "session not started";
}else{
 echo "session started";
}



Question: NOW() equivalent function in PHP?
echo date("Y-m-d H:i:s"); //2015-11-14 9:52:32



Question: How to get all php functions?
print_r(get_defined_functions());



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';