Which of the following will NOT add john to the users array?
1. $users[] = 'john';
Successfully adds john to the array
2. array_add($users,’john’);
Fails stating Undefined Function array_add()
3. array_push($users,‘john’);
Successfully adds john to the array
4. $users ||= 'john';
Fails stating Syntax Error
What’s the difference between sort(), assort() and ksort? Under what circumstances would you use each of these?
1. sort()
Sorts an array in alphabetical order based on the value of each element. The index keys will also be renumbered 0 to length - 1. This is used primarily on arrays where the indexes/keys do not matter.
2. assort()
The assort function does not exist, so I am going to assume it should have been typed asort().

Like the sort() function, this sorts the array in alphabetical order based on the value of each element, however, unlike the sort() function, all indexes are maintained, thus it will not renumber them, but rather keep them. This is particularly useful with associate arrays.
3. ksort()
Sorts an array in alphabetical order by index/key. This is typically used for associate arrays where you want the keys/indexes to be in alphabetical order.
What would the following code print to the browser? Why?
$num = 10;
function multiply(){
$num = $num * 10;
echo $num; // prints 10 to the screen

Since the function does not specify to use $num globally either by using global $num; or by $_GLOBALS['num'] instead of $num, the value remains 10.
What is the difference between a reference and a regular variable? How do you pass by reference and why would you want to?

Reference variables pass the address location of the variable instead of the value. So when the variable is changed in the function, it is also changed in the whole application, as now the address points to the new value.

Now a regular variable passes by value, so when the value is changed in the function, it has no affect outside the function.
$myVariable = "its' value";
Myfunction(&$myVariable); // Pass by Reference Example

So why would you want to pass by reference? The simple reason, is you want the function to update the value of your variable so even after you are done with the function, the value has been updated accordingly.
What functions can you use to add library code to the currently running script?

This is another question where the interpretation could completely hit or miss the question. My first thought was class libraries written in PHP, so include(), include_once(), require(), and require_once() came to mind. However, you can also include COM objects and .NET libraries. By utilizing the com_load and the dotnet_load respectively you can incorporate COM objects and .NET libraries into your PHP code, thus anytime you see "library code" in a question, make sure you remember these two functions.
What is the difference between foo() & @foo()?

foo() executes the function and any parse/syntax/thrown errors will be displayed on the page.

@foo() will mask any parse/syntax/thrown errors as it executes.

You will commonly find most applications use @mysql_connect() to hide mysql errors or @mysql_query. However, I feel that approach is significantly flawed as you should never hide errors, rather you should manage them accordingly and if you can, fix them.


How do you debug a PHP application?

This isn't something I do often, as it is a pain in the butt to setup in Linux and I have tried numerous debuggers. However, I will point out one that has been getting quite a bit of attention lately.

PHP - Advanced PHP Debugger or PHP-APD. First you have to install it by running:
pear install apd

Once installed, start the trace by placing the following code at the beginning of your script:

Then once you have executed your script, look at the log in apd.dumpdir.
You can even use the pprofp command to format the data as in:
pprofp -R /tmp/pprof.22141.0

For more information see http://us.php.net/manual/en/ref.apd.php
What does === do? What’s an example of something that will give true for ‘==’, but not ‘===’?

The === operator is used for functions that can return a Boolean false and that may also return a non-Boolean value which evaluates to false. Such functions would be strpos and strrpos.

I am having a hard time with the second portion, as I am able to come up with scenarios where '==' will be false and '===' would come out true, but it's hard to think of the opposite. So here is the example I came up with:
if (strpos("abc", "a") == true)
// this does not get hit, since "a" is in the 0 index position, it returns false.
if (strpos("abc", "a") === true)
// this does get hit as the === ensures this is treated as non-boolean.

How would you declare a class named “myclass” with no methods or properties?
class myclass

How would you create an object, which is an instance of “myclass”?
$obj = new myclass();

It doesn't get any easier than this.
How do you access and set properties of a class from within the class?

You use the $this->PropertyName syntax.
class myclass
private $propertyName;
public function __construct()
$this->propertyName = "value";

What is the difference between include, include_once? and require?

All three allow the script to include another file, be it internal or external depending on if allow_url_fopen is enabled. However, they do have slight differences, which are denoted below.
1. include()
The include() function allows you to include a file multiple times within your application and if the file does not exist it will throw a Warning and continue on with your PHP script.
2. include_once()
include_once() is like include() except as the name suggests, it will only include the file once during the script execution.
3. require()
Like include(), you can request to require a file multiple times, however, if the file does not exist it will throw a Warning that will result in a Fatal Error stopping the PHP script execution.


What function would you use to redirect the browser to a new page?
1. redir()
This is not a function in PHP, so it will fail with an error.
2. header()
This is the correct function, it allows you to write header data to direct the page to a new location. For example:
header("Location: http://www.search-this.com/");

3. location()
This is not a function in PHP, so it will fail with an error.
4. redirect()
This is not a function in PHP, so it will fail with an error.
What function can you use to open a file for reading and writing?
1. fget()
This is not a function in PHP, so it will fail with an error.
2. file_open()
This is not a function in PHP, so it will fail with an error.
3. fopen()
This is the correct function, it allows you to open a file for reading and/or writing. In fact, you have a lot of options, check out php.net for more information.
4. open_file()
This is not a function in PHP, so it will fail with an error.
What's the difference between mysql_fetch_row() and mysql_fetch_array()?

mysql_fetch_row() returns all of the columns in an array using a 0 based index. The first row would have the index of 0, the second would be 1, and so on. Now another MySQL function in PHP is mysql_fetch_assoc(), which is an associative array. Its' indexes are the column names. For example, if my query was returning 'first_name', 'last_name', 'email', my indexes in the array would be 'first_name', 'last_name', and 'email'. mysql_fetch_array() provides the output of mysql_fetch_assoc and mysql_fetch_row().
What does the following code do? Explain what's going on there.
print ereg_replace("([0-9]+)/([0-9]+)/([0-9]+)","\\2/\\1/\\3",$date);

This code is reformatting the date from MM/DD/YYYY to DD/MM/YYYY. A good friend got me hooked on writing regular expressions like below, so it could be commented much better, granted this is a bit excessive for such a simple regular expression.
// Match 0-9 one or more times then a forward slash
$regExpression = "([0-9]+)/";
// Match 0-9 one or more times then another forward slash
$regExpression .= "([0-9]+)/";
// Match 0-9 one or more times yet again.
$regExpression .= "([0-9]+)";

Now the \\2/\\1/\\3 denotes the parentheses matches. The first parenthesis matches the month, the second the day, and the third the year.
Given a line of text $string, how would you write a regular expression to strip all the HTML tags from it?

First of all why would you write a regular expression when a PHP function already exists? See php.net's strip_tags function. However, considering this is an interview question, I would write it like so:
$stringOfText = "

This is a test
$expression = "/<(.*?)>(.*?)<\/(.*?)>/";
echo preg_replace($expression, "\\2", $stringOfText);
// It was suggested (by Fred) that /(<[^>]*>)/ would work too.
$expression = "/(<[^>]*>)/";
echo preg_replace($expression, "", $stringOfText);

What's the difference between the way PHP and Perl distinguish between arrays and hashes?

This is why I tell everyone to, "pick the language for the job!" If you only write code in a single language how will you ever answer this question? The question is quite simple. In Perl, you are required to use the @ sign to start all array variable names, for example, @myArray. In PHP, you just continue to use the $ (dollar sign), for example, $myArray.

Now for hashes in Perl you must start the variable name with the % (percent sign), as in, %myHash. Whereas, in PHP you still use the $ (dollar sign), as in, $myHash.
How can you get round the stateless nature of HTTP using PHP?

The top two options that are used are sessions and cookies. To access a session, you will need to have session_start() at the top of each page, and then you will use the $_SESSION hash to access and store your session variables. For cookies, you only have to remember one rule. You must use the set_cookie function before any output is started in your PHP script. From then on you can use the $_COOKIE has to access your cookie variables and values.

There are other methods, but they are not as fool proof and most often than not depend on the IP address of the visitor, which is a very dangerous thing to do.
What does the GD library do?

This is probably one of my favorite libraries, as it is built into PHP as of version 4.3.0 (I am very happy with myself, I didn't have to look up the version of PHP this was introduced on php.net). This library allows you to manipulate and display images of various extensions. More often than not, it is used to create thumbnail images. An alternative to GD is ImageMagick, however, unlike GD, this does not come built in to PHP and must be installed on the server by an Administrator.
Name a few ways to output (print) a block of HTML code in PHP?

Well you can use any of the output statments in PHP, such as, print, echo, and printf. Most individuals use the echo statement as in:
echo "My string $variable";

However, you can also use it like so:
This text is written to the screen as output and this $variable is parsed too. If you wanted you can have HTML tags in here as well. The END; remarks must be on a line of its own, and can't contain any extra white space.

Is PHP better than Perl? - Discuss.

Come on, let's not start a flame over such a trivial question. As I have stated many times before,

"Pick the language for the job, do not fit the job into a particular language."

Perl in my opinion is great for command line utilities, yes it can be used for the web as well, but its' real power can be really demonstrated through the command line. Likewise, PHP can be used on the command line too, but I personally feel it's more powerful on the web. It has a lot more functions built with the web in mind, whereas, Perl seems to have the console in mind.

Personally, I love both languages. I used Perl a lot in college and I used PHP and Java a lot in college. Unfortunately, my job requires me to use C#, but I spend countless hours at home working in PHP, Perl, Ruby (currently learning), and Java to keep my skills up to date. Many have asked me what happened to C and C++ and do they still fit into my applications from time to time. The answer is primarily 'No'. Lately all of my development work has been for the web and though C and C++ could be written for the web, they are hardly the language to use for such tasks. Pick the language for the job. If I needed a console application that was meant to show off the performance differences between a quick sort, bubble sort, and a merge sort, give me C/C++! If you want a Photo Gallery, give me PHP or C# (though I personally find .NET languages better for quick GUI applications than web).

I would like to take this time to challenge other companies to post their interview questions or feel free to email them to me at search-this [at] cpradio [dot] org. I will be glad to read through them, and write an article about them revealing their answers for everyone to learn.

Changed # to % for Perl Hashes - Thanks to MrSpooky for pointing that out, can't believe I forgot that!


Q1.Can we write windows like applications in PHP.
Ans : Yes using PHP-GTK on linux and WinBinder on windows.

Q2.What difference does it make when I declare variables with $ and $ in prefix.
Ans: $x = "Lion";
$$x = "Zebra";
echo $Lion;
would display "Zebra"
Use : creating runtime variables

Q3.What is the difference between strpos and stripos function
Ans: strpos is case sensitive search, and stripos is case insensitive search

Q4.What are the ways by which we can find out if a variable has been declared?
Ans: isset or empty language constructs

Q5.What is "global" and how to use it?
Ans: variables declared outside the functions can be used inside the function using global keyword

Q6.What is the difference between echo and print
Ans: echo can take more than one parameter for displaying.
print cannot take more than one
echo 'This', 'That' //is valid
print 'This', 'That' //is invalid
print returns 1 always.
echo cannot be used to return anything
$ret = print "Abcd" //valid
$ret = echo "Abcd" //invalid


Q14.What does function `eval` do?
Ans: Evaluate a string as PHP code;
Eg. eval('echo "This would be printed"');

Q15.What is the method by which PHP converts datatype of a given variable.
Ans: settype()
$a = "10"; // $a is string
settype($a,"integer"); // $a is integer

Q.Can we change php.ini settings at the runtime, and how?
Ans : Yes, using ini_set();

Q16.What is the difference between sort(), assort() and ksort? Under what circumstances would you use each of these?
Ans: Sorts an array, sorts an array and maintains index association, Sorts an array by key
Simple sort (sorts on values)
Simple sort after sorting the array (lets assume size of array is 10) rearranges the index, if we want to access element at index[5], using a simple sort an element value would have changed, but in assort the value is still held by index 5 though its position in the array may be 10th.
Ksort – sorts the array by key and maintains the key to data correlations

Q17.What is the difference between foo() & @foo()?
Ans: if an error occurs calling foo() would show up the error on the screen, whereas, @foo() would suppress the error because ‘@’ is a error control operator.

Q18.What is the difference between mysql_fetch_row() and mysql_fetch_array() and mysql_fetch_object?
Ans: mysql_fetch_row() – fetches a row as an enumerated array
mysql_fetch_array() - Fetch a result row as an associative array, a numeric array, or both
mysql_fetch_object - Fetch a result row as an object

Q19.What is the difference between include & include_once? include & require?
Ans: include_once includes the script only once if it is already included in the execution of the script
Include includes the script everytime the include is encountered in the code
Require is identical to include except that it results in fatal error when file is not present in the include_path.

Q20.What type of inheritance PHP supports?
Ans: An object can inherit only from one base class. Multiple inheritance is not supported in PHP.

Q21.How can we call an object's method by using the variable functions?
Ans: If MyClass contains function myFunction()

$foo = new MyClass();
$var = "myFunction";

[ Resource Link : http://in.php.net/manual/en/functions.variable-functions.php ]

Ajax, Cross site scripting (JavaScript).

Q22.What is the use of AJAX?
Ans: If a page contains form that needs to be updated constantly based on runtime values, a javascript code constantly sends user input to the system and displays the results from the server, without refreshing the whole page.

Q23.What is cross site scripting, and how to avoid it?
Ans: Injecting a javascript code in the response of a form that is capable of calling and executing scripts from other site.

Occurs when variables holding form values are not cleaned with html code escaping functions.

Clean the variables before storing or before display. Recommendation is to display the code clean and store the value as it is, unless its specified otherwise.


Q1.What is a join, what types of join are supported in MySQL.
Ans: A ‘join’ joins two table in such a way that all or partial records are selected from both the table based on join criteria.

Joins supported in mysql are :







Q2.In MySQL, what table type is required for foreign keys to work?
Ans: innoDB

Q3.How does full text search work.
Ans: A table must be a myISAM table
Table must have char varchar and text columns
FULLTEXT index must be created at the time of creation of table

Q4.What is the use of files .frm, .MYD, .MYI
Ans: frm files store the table definition
MYD files store the data
MYI files store the index

Q5.What is maximum size of a database in MySQL?
Ans: 65+GB / table / [ limited by the OS]

Q6.How many columns can exist in a mySql table?
Ans: 4096 colums

Q7.What is the maximum size of a row in a mysql table?
Ans: 65,535 not including blobs (as these are stored separately)

Q8. What would you use if you have a choice Natural [left] join or inner join or left join with using clause?
Ans: The NATURAL [LEFT] JOIN of two tables is defined to be semantically
equivalent to an INNER JOIN or a LEFT JOIN with a USING clause that names all columns that exist in both tables


1. What are the differences between Get and post methods in form submitting, give the case where we can use get and we can use post methods?
Ans :-
In the get method the data made available to the action page ( where data is received ) by the URL so data can be seen in the address bar. Not advisable if you are sending login info like password etc. In the post method the data will be available as data blocks and not as query string in case of get method.

2. Who is the father of php and explain the changes in php versions?
Ans :-
Rasmus Lerdorf for version changes goto http://php.net/

3. How can we submit from without a submit button?
Trigger the JavaScript code on any event ( like onselect of drop down list box, onfocus, etc ) document.myform.submit();This will submit the form.

4. How many ways we can retrieve the date in result set of mysql using php?
As individual objects so single record or as a set or arrays.

5. What is the difference between mysql_fetch_object and mysql_fetch_array?
MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array

6. What is the difference between $message and $$message?
Both are variables only
$message is a variable and if used with print statement, the content of the $message variable will be displayed. Where as with $$message variable, the content of the $message will also be treated as variable and the content of that variable will be displayed. For ex: If $message contains "var", then it displays the content of $var on the screen.


7. How can we extract string 'abc.com ' from a string 'http://info@abc.com' using regular _expression of php?
preg_match("/^(http:\/\/info@)?([^\/]+)/i","http://info@abc.com", $data);
echo $data[2];

Use the function split split(“@”,”http://info@abc.com”) which returns an array any second element of the returned array will hold the value as abc.com.

8. How can we create a database using php and mysql?

9. 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.

10. Can we use include ("abc.php") two times in a php page "makeit.php"?
Yes we can include..

11. What are the different tables present in mysql, which type of table is generated when we are creating a table in the following syntax: create table employee(eno int(2),ename varchar(10)) ?
Total 5 types of tables we can create
2. Heap
3. Merge
MyISAM is the default storage engine as of MySQL 3.23.

Table Types are
· ISAM(Index Sequential Access Method)
o Static
o Dynamic
o Compress
· Merge
· Heap (Fastest tables because it stores in to the RAM)
· InnoDB (Transaction safe table)

When you fire the above create query MySQL will create the Dynamic table.
MyISAM Table Type is created, if u not specified any table type then default will be applied and MyISAM is default

13. How can I execute a PHP script using command line?
Through php parse you can execute PHP script using command line. By default location of php parser is /var/www/html so set the path of this directory and just use as following
#php sample.php

16. What is meant by nl2br()?
nl2br() inserts html break in string
echo nl2br(”god bless \n you”);

god bless

Returns string with ‘’ inserted before all newlines


20. How can we encrypt and decrypt a data present in a mysql table using mysql?
AES_ENCRYPT() and AES_DECRYPT() ---to encrypt string

21. How can we encrypt the username and password using PHP?
You can encrypt a password with the following

26. What are the different types of errors in PHP?
Notices: These are trivial, non-critical errors that PHP encounters while executing a script - for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all - although, as you will see, you can change this default behaviour.

Warnings: These are more serious errors - for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.

Fatal errors: These are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behaviour is to display them to the user when they take place.
27. What is the functionality of the function strstr and stristr?
string strstr ( string str1, string str2) this function search the string str1 for the first occurrence of the string str2 and returns the part of the string str1 from the first occurrence of the string str2. This function is case-sensitive and for case-insensitive search use stristr() function.

28. What are the differences between PHP 3 and PHP 4 and PHP 5?
for this ans goto http://php.net and check the version changes

29. How can we convert asp pages to PHP pages?
You can download asp2php front end application from the site http://asp2php.naken.cc.

30. What is meant by urlencode and urldecode?
string urlencode(str)
where str contains a string like this "hello world" and the return value will be URL encoded and can be use to append with URLs, normaly used to appned data for GET like someurl.com?var=hello%world
string urldocode(str)
this will simple decode the GET variable’s value
Like it echo (urldecode($_GET_VARS[var])) will output "Hello world"

34. What is the difference between the functions unlink and unset?
unlink is a function for file system handling. It will simply delete the file in context
unset will set UNSET the variable. e.g

35. How can we register the variables into a session?
Yes we can


42. How many ways can we get the value of current session id?
session_id() returns the session id for the current session.

43. How can we destroy the session, how can we unset the variable of a session?
session_unregister -- Unregister a global variable from the current session
session_unset -- Free all session variables

44. How can we destroy the cookie?
Set the cookie in past

45. How many ways we can pass the variable through the navigation between the pages?
GET or QueryString and POST

46. What is the difference between ereg_replace() and eregi_replace()?
eregi_replace() function is identical to ereg_replace() except that this ignores case distinction when matching alphabetic characters.eregi_replace() function is identical to ereg_replace() except that this ignores case distinction when matching alphabetic characters.

47. What are the different functions in sorting an array?
Sorting functions in PHP,

48. How can we know the count/number of elements of an array?
2 ways
a) sizeof($urarray) This function is an alias of count()
b) count($urarray)
interestingly if u just pass a simple var instead of a an array it will return 1.


53. List out the predefined classes in PHP?
1. Standard Defined Classes
These classes are defined in the standard set of functions included in the PHP build.

a. Directory
The class from which dir() is instantiated.


2.Ming Defined Classes
These classes are defined in the Ming extension, and will only be available when that
extension has either been compiled into PHP or dynamically loaded at runtime.


b. swffill

c. swfgradient

d. swfbitmap

e. swftext

f. swftextfield

g. swffont

h. swfdisplayitem

i. swfmovie

j. swfbutton

k. swfaction

l. swfmorph

m. swfsprite

3. Oracle 8 Defined Classes
These classes are defined in the Oracle 8 extension, and will only be available when
that extension has either been compiled into PHP or dynamically loaded at runtime.

a. OCI-Lob
b. OCI-Collection

4. qtdom Defined Classes
These classes are defined in the qtdom extension, and will only be available when that
extension has either been compiled into PHP or dynamically loaded at runtime.

a. QDomDocument

b. QDomNode

56. How can we send mail using JavaScript?
No You can't send mail using Javascript but u can execute a client side email client to send the email using mailto: code.

Using clientside email client
function myfunction(form)
return true;

This question is wrong. You aren’t really ’sending mail’ when doing a ‘mailto’ and so it’s a misleading question… A smart candidate would just say “It’s not possible” and you may write him off.

57. What is meant by PEAR in php?
PEAR is the next revolution in PHP. This repository is bringing higher level programming to PHP. PEAR is a framework and distribution system for reusable PHP components. It eases installation by bringing an automated wizard, and packing the strength and experience of PHP users into a nicely organised OOP library. PEAR also provides a command-line interface that can be used to automatically install "packages"

58. What is the purpose of the following files having extensions 1) frm 2) MYD 3) MYI. What these files contains?
In MySql, the default table type is MyISAM.
Each MyISAM table is stored on disk in three files. The files have names that begin with the table name and have an extension to indicate the file type.
The `.frm' file stores the table definition.
The data file has a `.MYD' (MYData) extension.
The index file has a `.MYI' (MYIndex) extension,


Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.

60. What is the difference between echo and print statement?

echo() can take multiple expressions,Print cannot take multiple expressions.

echo has the slight performance advantage because it doesn't have a return value.


1) Among the following which one returns the value of a field in a recordset?

a) mysql_field_table()
b) mysql_field_name()
*c) mysql_result()
d) mysql_list_fields()

2) Which function returns column information from a recordset as an object ?

a) mysql_fetch_object()
*b) mysql_fetch_field()
c) mysql_field_name()
d) mysql_fetch_row()

3) Which function returns MySQL host info ?

*a) mysql_get_host_info()
b) mysql_host()
c) mysql_host_info()
d) mysql_get_host()

4) What is the use of the function mysql_info() ?

a) Returns the information about mysql server
b) Returns the current status the of the mysql server
*c) Returns the information about the last query
d) None of the above

5) Which function returns the error number of the last MySQL operation ?

a) mysql_num_error()
b) mysql_error_num()
c) mysql_error()
*d) mysql_errno()

6) Which function returns length of each output in a result ?

a) mysql_field_len()
*b) mysql_fetch_lengths()
c) mysql_fetch_length()
d) mysql_field_length()

7) What is the second parameter does in the function mysql_field_table(param1,param2) ?

a) Database connection resource id
*b) The numerical field offset
c) Result set
d) Database name

8) Which function checks whether or not the connection to the server is working ?

a) mysql_ stat()
b) mysql_ get_ server_ info()
*c) mysql_ping()
d) mysql_ pconnect()

9) How to fetch a result row as an associative array ?

*a) mysql_fetch_assoc()
b) mysql_ fetch_ row()
c) mysql_ fetch_ field()
d) None of these

10) What is the return value of the function mysql_select_db() in case of success ?

a) Link Identifier
b) Resource
c) Array of strings
*d) True