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?
$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?
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;
$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 http://us.php.net/manual/en/ref.apd.php
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.