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:"); //Redirect to home page of another website

header('location:"); //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:', 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?
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
setcookie('no_array', $arrayData);

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

Question 4: What is the difference between bindParam and bindValue?
$string='this is ';
if (strlen($string) != strlen(utf8_decode($string)))
    echo 'is unicode';
  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 = "";
$path = parse_url($url);
    [scheme] => http
    [host] =>
    [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);
/**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"));

/*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);

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