31 Aug 2014

10 Always Asked PHP Interview Questions and Answers

Q1: What is the difference between PHP 4 and PHP 5?

Ans: One should know the following changes to understand what has changed from PHP 4 to PHP 5.
  • PHP 4 is more of a procedural Language (but it had support for OOP) whereas PHP 5 is an object oriented language (it has more enhanced features of OOP like access specifiers, inheritance etc.). 
  • PHP5 removed register_globals, magic quotes, and safe mode. 
  • PHP 5 introduced many new things like –interfaces, functions, error level (E_STRICT), default extensions (like PDO, SimpleXML, etc.), MySQL extension named MySQLi (Improved MySQL), built in SOAP extension for interoperability with web services. 
  • PHP 5 gave 3 levels of accesses – Public,Private and Protected – which were not in PHP 4 earlier. 
  • PHP 4 was powered by Zend Engine 1.0, while PHP 5 is powered by Zend Engine 2. 
  • Major difference is PHP 5 got a new Object Model.In PHP 4 objects were handled as primitive types (everything was passed by value, including objects) while in PHP 5 objects are referenced by handle, and not by value (objects are passed by reference) 

Q2: What is the difference between $var and $$var? When is $$var used?

Ans: $var is a simple variable. Variables are declared in PHP using the $. So any variable can be declared as $x, $y, etc.
$$var instead is a reference variable. Consider the following example.
$var = “mango”;
$$var = 10;
echo $mango;// output: 10
  $$var is used when you need flexible variable naming.

Q3: What is the difference between require, require_once, include and include_once?

Ans: 
Difference between require and include:
Include: Used to include a file in a PHP code, but upon failure it generates a warning (E_WARNING) and the script will continue, the code will run.
Syntax: include 'filename'; or include(“filename”);

Require: Also used to include a file in a PHP code, but upon failure it generates an error (E_COMPILE_ERROR) and the script will halt.
Syntax: require 'filename'; or require(“filename”);
Remember r & r, error = require.

Difference between require_once and require:

require_once is identical to require the only difference being that if require_once is used, PHP will check if the file has already been included, and if so, it will not include it again.
Syntax: require_once ‘filename’; or require_once(“filename”);
include_once ‘filename’; or include _once(“filename”);
Note: Better to use include ‘filename’ instead of bracket () version;

Q4: What is the difference between GET and POST method?

Ans: These are the two most commonly used HTTP request methods.
GET: It requests data from the source.
POST: it submits data to be processed to a specific source.
Parameters
GET
POST
Caching
GET requests can be cached.
POST request never cached.
Data
Limited data can be sent like 2 kb.
Unlimited
Security
Data shown in url, non-secure
Totally secured
Browser History
GET requests remains in browser history
POST requests don’t.
Safe for password/sensitive data sending?
No
Yes
Can be bookmarked?
Yes
No

Q5: What are various ways of sending data from browser to web server and vice versa?

Ans: Data could be send in many ways.

From browser to web server:

  1. Query string (www.example.com?abc=xyz)
  2. POST method ($_POST)
  3. Sessions ($_SESSION)

From Server to Browser:

  1. GET method ($_GET)
  2. Cookies (A cookie, also known as an HTTP cookie, web cookie, or browser cookie, is a small piece of data sent from a website and stored in a user's web browser while the user is browsing that website. Every time the user loads the website, the browser sends the cookie back to the server to notify the website of the user's previous activity)

Q6: What are commonly used HTTP status codes?

Ans: The commonly used HTTP status codes which you will encounter in your daily life are the following:
  • 200 – ok
  • 301 – Moved Permanently
  • 302 – Found
  • 303 – See other
  • 403 – Forbidden
  • 404 – Not found
  • 500 – Internal Server error
  • 502 – Bad Gateway
  • 503 – Service unavailable
There are actually 5 classes of response:
  • 1xx Informational (Request received, continuing process)
  • 2xx Success (Client action was received, understood, accepted & processed successfully)
  • 3xx Redirection (further action needs to be taken to complete the request)
  • 4xx Client error
  • 5xx Server error

Q7: What are various common HTTP header fields?

Ans: HTTP header fields provide information about the request, response or about the object sent in the message body. The common HTTP header fields are:

Client Request headers:

  • Cookie (The Cookie request-header field value contains a name/value pair of information stored for that URL. E.g. Cookie: name=value)
  • Authorization (it contains the authentication credentials for HTTP authentication. E.g. Authorization : credentials)

Server Response headers:

  • Location (used in redirection, or when a new resource has been created)
  • Refresh (used in redirection, or when a new resource has been created.)
E.g. syntax: header("refresh:5;url=wherever.php");
This syntax redirects to the desired page after 5 seconds.
E.g. syntax: header("refresh:0;url=wherever.php");
This syntax redirects to the desired page immediately.

Q8: What is .htaccess and why it is used?

Ans: 
  • .htaccess is directory-level configuration file.
  • The original purpose of .htaccess was to allow per-directory access control. For example requiring a password to access the content.
Common usages:
  • Authorization & authentication.
  • Rewriting URLs.
  • Block users by IP address or domain.
  • Allow a server to control caching by web browsers and proxies.
  • Customized error responses.
  • Enable server-side includes.

Q9: Describe about the different types of error in PHP?

Ans: There are 3 types of errors which are very common in PHP.
  1. Notices: These are run-time notices. These occur if the script found something that might be an error. These errors are not displayed to the user. E.g. Accessing a variable that has not yet been defined.
  2. Warnings: These are serious errors but non-fatal. These errors are by default displayed to the user but they don’t result in script termination. E.g. Attempting to include() a file which does not exist will generate a warning.
  3. Errors: These are fatal and critical errors. They result in script termination and are by default displayed to the user. E.g. Instantiating an object of a non-existent class, or calling a non-existent function.

Q10: What is SESSION and how it is set and unset?

Ans: 
  • Session is a simple way to store data for individual users against a unique session ID.
  • It persist state information between pages.
  • $_SESSION is the global variable which gets set when session is started.
  • Manually sessions can be started using session_start() function.
Registering a variable:
<?php
session_start();
$_SESSION[‘key’] = value;

?>
Un-registering a variable:
<?php
session_start();
unset($_SESSION[‘key’]);

?>
Completely destroy/reset all session data:
<?php
session_destroy();
?>

0 comments:

Post a Comment