PHP common interview questions – answers

=,==,=== – what is the difference between these?
= assigns a value, == checks if value is the same, === checks if value is the same and the variables are of the exact same type.
Echo, print, printf – what is the difference between these?
Print and echo both output what is passed to them. Print acts like a function, so you can use it in complex statements. Printf is used to format the output.
Include, include once, require – what is the difference between these?
Include will includes a file each time it is called. Include_once would only include a file one time, so if a php program has a file in two include_once statements only the first will be done. Requre is like include, but if the file included is not available a fatal error occurs and processing stops.
Are variables passed to functions by reference or value?
A variable is passed by value unless the variable is passed with an &, such as functionName(&$variableName)
How do you encrypt data?
The PHP md5() function is very pretty widely used. The PHP crypt() function can also be used to encrypt data. (Honestly, the one time I had this question I drew a complete blank, so I’m not exactly certain what they were looking for.)
What editor or ide do you use?
Interesting question, probably does say something about your programming capabilites.  use Eclipse 3.1 with PHP extensions, , Zend Studio  Serious Unix users would be good with vi or emacs.
PHP  Static, what are they and how do you use them?
As with Java, if the job is for OO PHP (that is PHP 5), there will be a question on PHP static variables. You may be asked how to reference a static from inside and outside of the class it is in, or just show that you get the basic concept of a variable or function that is for the whole class and not an instance.
Design Patterns general questions
Now we are really into OO stuff. As in java job interviews I’ve usually been asked about design patterns in fairly vague terms, such as “describe the design patterns used in the systems you have worked on”. I was asked that so much that I drew out some UML to bring with me to interviews, as I found drawing up the UML during the interview to be distracting and slow.
How can I maintain the count of how many persons have hit my site?
The IP of a visitor can be tracked by
Just insert this value with date into a table.
What is difference between mysql_fetch_array(), mysql_fetch_row() and mysql_fetch_object()?
mysql_fetch_array()– Fetche resultset from a table in the form of array
Eg: $res= mysql_query(’select * from tbl’)
while ($row=mysql_fetch_array($res))
Fetch single row
Fetches resultset as object
How I can make a chat page in php in simple
On click on chat submit button store the values in the text area to a text file using PHP file functions.
you can use ajax fore improved user experiance.
wrie another javascript + ajax function to call the contents from the text file in each 3 sec

What is the difference between echo and print statement?
Print is a function and returns a value. Using echo we can use more than one parameter
echo “and a “, 1, 2, 3; 
What is differenc between mysql_connect and mysql_pconnect?
mysql_connect- Creates a connection with db
mysql_pconnect- creates a persistant connection with db 
How I can get IP address of the user?
)$ip = $_SERVER[‘REMOTE_ADDR’]; 
How I will check that user is logged in or not?
store user information in session

I want to make it a function and I want to use it in each page and after login I want to go in current page?
)store url in a variable and put in session before
After login redirect page to that url 
How do we know browser properties?
If you have to work with dates in the following format: “Tuesday, February 14, 2006 @ 10:39 am”, how can you convert them to another format, that is easier to use?
first remove @ from the string and than use strtime to convert this to unix time stamp.
What is difference between require_once(), require(), include()?
all are for including afile into another file
require- if fails shoot a fatal error
include- if fails shoot a warning
include/require_once- for make sure that the page is included once in another page


PHP Interview Questions and Answers list

What are the Technology to Use for Parsing the XML ?
There are two types of XML parsers the we can use.
1. DOM (Document Object Module).
2. SAX (Simple API for XML).
The DOM (Document Object Model) extension allows to operate on XML documents through the DOM API with PHP 5. DOM is a standard defined by the W3C for accessing XML documents.
In PHP 4, DOM xml extension is not following the standard method names.
As per the new W3C compatibility, the old dom fxml-based scripts won’t work anymore. The API is quite different in PHP 5. But if we used the “almost W3C compatible” method names available in PHP 4.3, We only need to change the loading and saving methods, and remove the underscore in the method names. Other adjustments here and there may be necessary, but the main logic can stay the same. Though we have not used earlier so these will not be a problem for us.
The easiest way to read a well-formed XML file is to use the DOM library . The DOM library reads the entire XML document into an object and represents it as a tree of nodes,
SAX stands for Simple API for XML. It’s a callback-based interface for parsing XML documents. SAX support has been available since PHP 3 and hasn’t changed a lot since then. For PHP 5 the API is unchanged, The only difference is that it’s not based on the expat library anymore, but on the libxml2 library.
Rather than treating an XML document as a tree-like structure, SAX treats it as a series of events such as startDocument or endElement. To accomplish this, a SAX appllication consists of a parser that sends these events to “handlers,” methods or functions designated to handle them.
If you need a superfast forward only XML parser, you can use XMLReader. However you probably don\’t want a forward only parser.
If you want to read data from an XML file, the simple XML extension is probably the nicest solution. Also for creating XML, it might work well for you. Have a look at this article:
If you want a superfast forward only XML writer, use XMLWriter. You could also do this by just outputting the XML of sticking it all together in a string. But the XMLWriter will make your code clearer, automatically escape stuff and give some error indications.
How can we get second of the current time using date function?  what is Magic methods in php?

There are seven special methods, and they are as follows:
__construct( )  Called when instantiating an object
__destruct( ) Called when deleting an object
__get( ) Called when reading from a nonexistent property
__set( ) Called when writing to a nonexistent property
__call( ) Called when invoking a nonexistent method
__toString( ) Called when printing an object (for eg: converting an object to strings)
__clone( ) Called when cloning an object (copying object)
There are actually 12 special methods at last count
The function names __construct, __destruct (see Constructors and Destructors), __call, __get, __set, __isset, __unset (see Overloading), __sleep, __wakeup, __toString, __set_state and __clone
what is scandir() ?

List files and directories inside the specified path By default files order will be ascending
$f = scandir($direct, 1); it will display the files as descending order
Which types of form elements can be excluded from the HTTP request?
A. text, radio, and check box
B. text, submit, and hidden
C. submit and hidden
D. radio and check box

Answer D is correct.
When not selected, both radio buttons and check boxes are excluded from the HTTP request. Answer A, C, B are incorrect because they are always included in the request.

 Is PHP a case sensitive programming language?
 PHP is a partially case sensitive programming language. We can use function names, class names in case insensitive manner.
What is mean by LAMP?
 LAMP means combination of Linux, Apache, MySQL and PHP.
 How do you get the user’s ip address in PHP?
 Using the server variable: $_SERVER[’REMOTE_ADDR’]
 What is the difference between require and include?
 When using require function to embed another file in php, it will give fatal error if the file is not exists.
 When using include function to embed another file in php, it will give warning if the file is not exists.

How to find the number of elements in an array?

 Using count($array) or sizeof($array).
 How do you make one way encryption for your passwords in PHP?
 Using md5 function or sha1 function
 How do you get ASCII value of a character?
 By using ord function.


important PHP interview questions and answers

Here is some PHP Interview Questions, that will helpfull to PHP Interview Candidates
1. What is CAPTCHA?
CAPTCHA stands for Completely Automated Public Turing Test to tell Computers and Humans Apart. To prevent spammers from using bots to automatically fill out forms, CAPTCHA programmers will generate an image containing distorted images of a string of numbers and letters. Computers cannot determine what the numbers and letters are from the image but humans have great pattern recognition abilities and will be able to fairly accurately determine the string of numbers and letters. By entering the numbers and letters from the image in the validation field, the application can be fairly assured that there is a human client using it. To read more look here:
2. What is difference between require_once(), require(), include().
Becouse above three function usely use to call a file in another file?

Difference between require() and require_once(): require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page). So, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don’t include the file more times and you will not get the “function re-declared” error. Difference between require() and include() is that require() produces a FATAL ERROR if the file you want to include is not found, while include() only produces a WARNING. There is also include_once() which is the same as include(), but the difference between them is the same as the difference between require() and require_once().
3. If you have to work with dates in the following format: “Tuesday, February 14, 2006 @ 10:39 am”, how can you convert them to another format, that is easier to use?
The strtotime function can convert a string to a timestamp. A timestamp can be converted to date format. So it is best to store the dates as timestamp in the database, and just output them in the format you like.
So let’s say we have
$date = “Tuesday, February 14, 2006 @ 10:39 am”;
In order to convert that to a timestamp, we need to get rid of the “@” sign, and we can use the remaining string as a parameter for the strtotime function.
So we have
$date = str_replace(“@ “,””,$date);
$date = strtotime($date);
now $date is a timestamp
and we can say:
echo date(“d M Y”,$date);
4. How we know browser properties?
get_browser() attempts to determine the capabilities of the user’s browser. This is done by looking up the browser’s information in the browscap.ini file.

$browser = get_browser();
foreach ($browser as $name => $value) {
echo “$name $value
5. How i will check that user is, logged in or not. i want to make it a function and i want to use in each page and after login i want to go in current page(same page. where i was working)?
For this we can use the session objec($_SESSION)t. When the user login with his/ her user name and password, usually we check those to ensure for correctness. If that user name and password are valid one then we can store that user name in a session and then we can very that session variable has been set or not in a single files and we can include that file in all pages.
6. How i can get ip address?
We can use SERVER var $_SERVER[‘SERVER_ADDR’] and getenv(“REMOTE_ADDR”) functions to get the IP address.
7. What is difference between mysql_connect and mysql_pconnect?
mysql_pconnect establishes a persistent connection. If you don’t need one (such as a website that is mostly HTML files or PHP files that don’t call the db) then you don’t need to use it. mysql_connect establishes a connection for the duration of the script that access the db. Once the script has finished executing it closes the connection. The only time you need to close the connection manually is if you jump out of the script for any reason.
If you do use mysql_pconnect. You only need to call it once for the session. That’s the beauty of it. It will hold open a connection to the db that you can use over and over again simply by calling the resource ID whenever you need to interact with the db.
8. What is the difference between echo and print statement?
There is a slight difference between print and echo which would depend on how you want to use the outcome. Using the print method can return a true/false value. This may be helpful during a script execution of somesort. Echo does not return a value, but has been considered as a faster executed command. All this can get into a rather complicated discussion, so for now, you can just use whichever one you prefer.
9. How to make a download page in own site, which i can know that how many file has been loaded by particular user or particular ip address?
We can use hyperlink having URL where file are kept. and we only allow regisetered user to download. from session of user we can get the user detail
10) What is the difference between inner join and outer join?

Inner join displays rows from table where the data is available in both the tables, where in outer join we can configure it to bring out rows from one table where the data is missing in other table for the corresponding rows.
Inner join
Say you have one table of CUSTOMERS and one table of ORDERS. Each row in the ORDERS table has a reference (foreign key reference) to a customer id which represents what customer placed that order. If you want to run a query that lists the orders along with the names of the customers who ordered them (since a customer id number itself it pretty useless), you will want to execute a join query:
Outer join
If for some reason, you wanted the query results to return all customer name regardless of whether they placed an order, you can use one of two types of OUTER JOINS, in this case, a LEFT JOIN: