Tuesday 9 October 2012

PHP Interview Questions and Answers for fresher

PHP Interview Questions and Answers for fresher

Question: What is PHP?
php interview questions and answers

PHP is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

Question: What is name of function that allows you to store session data in a database?
Answer: session_set_save_handler

Question: What is obj_start()?
Answer:It is used to initialize the object buffer, so that whole page first parsed instead of parsing in parts and send to browser.

Question: Why whitelists afford stronger protection against attacks than blacklists?
Answer: Blacklists words are some bad words (like s*x, f**k etc) that are not allowed to input by users into application. Except blacklists words, all words are allowed to input into the database.
whitelists words are those words that only allowed to input by user in application. Except Whitelists, all words removed while input the data into database.

Question: What is Session Register?
Answer: It is used to register a one or more global variable with current session. It session_start() was not called then it will call automatically when you call session_register;

Question: What are new Featues in PHP5?
Answer:  Type hinting
                Magic method and Magic constants
                Standard PHP Libarary (SPL)
                Access Modifier (Public, Privated & Protected)
                Changes in Functions
                Heredoc introduced
                E_STRICT : warning for deprecated function
                2nd option is optional in ternary operator

Question: List the Error constants in php?

Question: What are the advantages of stored procedures, triggers, indexes in PHP?
AnswerStored Procedure: It is set of sql command that can be compiled and stored in the server. Once this has been done, clients don't need to keep re-issuing the entire query. this improve the performance because the query has to be parsed only once.

Question: What is difference between cookie and session?
AnswerSession stored in server side and cookie stored in client side.
Session is logical object that store the object OR data and preserver on navigation of multiple page.
Cookie is stored temporary in browser (like mozilla, IE) and expired after close the browser.

Question: What is Persistent Cookie ?
AnswerPersistent cookie stored in browser and don't expired after close the browser. It retain the information until manually removed the cookie.

Question: Is overloading possible in PHP? How?
AnswerYes, overloading is possible. It is possible through Magic method

Question: Name of php function that Counts the number of occurrences of ever character.

Question: What is cache?
Answer: It is high speed access protected area in a computer which is reserved for temporary storage of data. It can be divided into two parts.
A) Cache Memory - It is more fast access area(Static RAM).
B) Disk Cache - Cache stored in disk (Dynamic RA)

Question: How session is working?
Answer: When we create a session, it stored in server side but store the protected key in browser cookie.
That's why most of sites stop login functionality when you disable the cookie.

Question: What is PHPUnit?
Answer: It is framework, written in PHP used testing the code.

Question: How to Store Session in databases?.
Answer: To store the session in database, we have to get access whenever session is created, read, write, destroy. For this their is one function in php session_set_save_handler

bool session_set_save_handler ( callable $open , callable $close , callable $read  , callable $write , callable $destroy , callable $gc )

Just pass the 6 function name, that will be call automatically whenever you start using session.

Question: Get the Images source from the html source?
$htmlSource = ".......";
$imageArray = array();
$doc = new DOMDocument();
$doc->loadHTML( $htmlSource  );

$tags = $doc->getElementsByTagName('img');

foreach ($tags as $tag) {
       $imageArray[] = $tag->getAttribute('src');

Question: What is traits in PHP?

Answer: PHP5.4 implements a method of code reuse called Traits.

Question: Difference between Notice, warning and Error ?
Answer: NOTICE: this is a short message for saying what to do or what not to do. For Example, When you are trying to use undefined varaible
echo $noExist; 
WARNING: occcurs at run time. Code execution continues. more serious as compare to warning. For Example, When you are trying to include a noexistfile
 echo include "notexist.php"; 
ERROR: this also occurs at run time, execution terminates. Its more serious to above both. For Example, When you are trying to use require a noexistfile
 echo require "notexist.php"; 

Question: How to set cookie
Answer: Use setcookie function to set the cookie. you can set the cookie name, value, expiration date, domain, secure, httponly.

Question: What does a special set of tags <?= and ?> do in PHP?
Answer: The output is sent directly to the browser.

Question: What is default port of FTP?
Answer: 21

Question: What is default port of SFTP?
Answer: 22

Question: What is default port of SMTP?
Answer: 25

Question: What is default port of MYSQL?
Answer: 3306

Question: What is default port of HTTP?
Answer: 80

Question: How to set and get a constant value?
/*set a value as constant*/
define("MAXSIZE", 100);

/*get a value of constant*/
echo constant("MAXSIZE")

Question: What does do ob_start();
Answer: ob_start Function is used to turn on output buffering.The PHP output buffering will save all the server outputs to a string variable.it  returns TRUE on success or FALSE on failure.If you are using ob_start, then you should also use ob_end_flush  function for flushing the output buffer. You can also use a call-back function like ob_start("function_name");

Question: What is Difference JSON and JSONP
Answer: JSONP is JSON with padding, that is, you put a string at the beginning and a pair of parenthesis around.

//JSON format
{"name":"Rob Allen","id":5}
//JSONP format
func({"name":"Rob Allen","id":5});

Question: List out the predefined classes in PHP?
Answer: Following are some predefined class in PHP.
  • Directory
  • stdClass
  • __PHP_Incomplete_Class
  • exception
  • php_user_filter

Question: What is the difference between session_register and $_session?
Answer: If session_start() was not called before session_register() is called, an implicit call to session_start() with no parameters will be made. But $_SESSION requires session_start() before use.
session_register function returns boolean value and $_SESSION returns string value
session_register() function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0.

Question: How to Change the Session name?
Answer: session_name — Get and/or set the current session name.

Question: What is JSON?
Answer: Full form of JSON is JavaScript Object Notation. It is a lightweight data-interchange format, which is independent of operating system and languages. Its similar to XML, but better in performance like smallar and fast.

Question: How do you capture audio/video in PHP?
Answer: FFmpeg is a complete solution to record, convert and stream
audio and video.

Question: How to prevent form hijacking in PHP?
Answer: Use token system in form.

Question: What is the difference between mysql_fetch_object, mysql_fetch_array and mysl_fetch_row?
mysql_fetch_array() - Fetch a result row as an associative array, a numeric array, or both
mysql_fetch_assoc() - Fetch a result row as an associative array
mysql_fetch_row() - Get a result row as an enumerated array
mysql_data_seek() - Move internal result pointer
mysql_query() - Send a MySQL query
mysql_fetch_object: Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.

Question: What are the differences between require and include, include_once?
File will not be included more than once. If we want to include a file once only and further calling of the file will be ignored then we have to use the PHP function include_once().

This will prevent problems with function redefinitions, variable value reassignments, etc.

Question: What is meant by nl2br()?
Answer: Returns string with '<br />' or '<br>' inserted before all newlines (\r\n, \n\r, \n and \r).

Question: How to set the response code in PHP?

Question: How to get the response code in PHP?
echo http_response_code();//404