Sunday, 15 November 2015

Top 10 PHP Interview Questions and Answers


Top 10 PHP Interview Questions and Answers


Question 1: How to redirect a page to another?
You can use header function to recirect.
header('location: http://www.web-technology-experts-notes.in"); //Redirect to home page of another website

header('location: http://www.web-technology-experts-notes.in/2015/11/php-questions-and-answers-for-experienced.html"); //Redirect to another page and another website

header('location: php-questions-and-answers-for-experienced.html"); //Redirect to another page of same website

header('location: php-questions-and-answers-for-experienced.html?topic=php");  //Redirect to another page of same website with parameter

header('Location: http://www.web-technology-experts-notes.in/2015/11/php-questions-and-answers-for-experienced.html', true, 301); //Permanent Redirect

Following are different use of header functions.
  1. Redirect from one page to another page. Also can redirect to another website.
  2. Help to download the file like (csv download, pdf download etc).
  3. Browser Authentication to the web page.
  4. Set the header of any page at the top of page.



Question 2: Is it secure to store a password in a session?
No,
If you still need then please stored in encrypted form with different name (not password).


Question 3: How to set/get PHP array in cookie?
//Set an array in cookie
$arrayData=json_encode(array(1,2,3,4,5,6));
setcookie('no_array', $arrayData);

//Get an array from cookie
$cookie = $_COOKIE['no_array'];
$arrayData = json_decode($cookie);
print_r($arrayData);



Question 4: What is the difference between bindParam and bindValue?
$string='this is ';
if (strlen($string) != strlen(utf8_decode($string)))
{
    echo 'is unicode';
}else{
  echo 'It is Not unicode.';
}



Question 5: How to add 30 mins to a date?
$date = date("Y-m-d H:i:s");
date("Y/m/d h:i:s", strtotime("+30 minutes", $date));



Question 6: How to get Path From URL?
$url = "http://www.web-technology-experts-notes.in/2013/09/amazon-s3-introduction.html";
$path = parse_url($url);
print_r($path);
/**Array
(
    [scheme] => http
    [host] => www.web-technology-experts-notes.in
    [path] => /2013/09/amazon-s3-introduction.html
)*/



Question 7: How to get array elements which present in two different array?
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
/**Array ( [a] => green [0] => red )*/



Question 8: How to replace array elements with another another?
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
print_r($input);

/*Array ( [0] => red [1] => green [2] => blue [3] => black [4] => maroon )*/



Question 9: Count number of elements in an array?
$foodArray = array('fruits' => array('orange', 'banana', 'apple'),
              'veggie' => array('carrot', 'collard', 'pea'));

echo count($foodArray); // output 2
// recursive count
echo count($foodArray, COUNT_RECURSIVE); // output 8



Question 10: How to exchanges all keys with their associated values in an array?
$input = array("oranges", "apples","mango", "pears");
$flippedArray = array_flip($input);

print_r($flippedArray);
/*Array ( [oranges] => 0 [apples] => 1 [mango] => 2 [pears] => 3 )*/



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());