Priti's Technical Corner

March 25, 2009

PHP Interview Question

Filed under: PHP — Tags: , — pritisolanki @ 8:47 am

Iqbal Husain on PHP Questions Shared his valuable interview experience with us.I have simply formatted the content/experience provided by Iqbal. Enjoy !!

Question 1: Which of the following will NOT add john to the users array?
Answer 1:
$users[] = ‘john’; -> Successfully adds john to the array
array_add($users,’john’); -> Fails stating Undefined Function array_add()
array_push($users,‘john’); -> Successfully adds john to the array
$users ||= ‘john’; -> Fails stating Syntax Error

Question 2: What’s the difference between sort(), assort() and ksort? Under what circumstances would you use each of these?
Answer 2:
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.

assort(): The assort function does not exist, so I am going to assume it should have been typed asort().
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.

Question 3: What would the following code print to the browser? Why?
$num = 10;
function multiply(){
$num = $num * 10;
echo $num; // prints 10 to the screen

Answer 3: 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.

Question 4: What is the difference between a reference and a regular variable? How do you pass by reference and why would you want to?
Answer 4: 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.

Question 5:What functions can you use to add library code to the currently running script?
Answer 5: 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.

Question 6: What is the difference between foo() & @foo()?
Answer 6: 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.

Question 7: How do you debug a PHP application?
Answer 7: 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

I welcome all readers to help each other my sharing their interview experience here.If you been ask some tuff question and searching answers then drop in here I will try to answer them.I again express my thanks to Iqbal for sharing his experience with us.


1 Comment »

  1. i am in trouble. please help me by solving following query

    that receives two parameters, an associative array ($find = array(‘key1’ => ‘value1’, ‘key2’ => ‘value2’ …) and a string ($string) and replaces occurrences of the keys of $find in $string with the corresponding values of $find in the order in which $find is defined.

    The algorithm SHOULD NOT consider ANY replacement text for further replacement.
    For example, if $find = array(‘abc’ => ‘cd’, ‘cde’ => ‘efg’) and $string = ‘cdeabcecde’, the algorithm should find ‘abc’ and replace it with ‘cd’ but should NOT subsequently consider any part of the replacement ‘cd’ when searching for the next key ‘cde’. The resulting string should be ‘efgcdeefg’. The algorithm should return the result string.
    Your solution will preferably be a single function.
    Use of built-in PHP array and string functions is encouraged.


    Comment by shakti singh — March 25, 2009 @ 9:41 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: