| 2 comments ]

1.
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
2.
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().

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.
3.
What would the following code print to the browser? Why?
PLAIN TEXT
PHP:
1.
$num = 10;
2.
function multiply(){
3.
$num = $num * 10;
4.
}
5.
multiply();
6.
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.
4.
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.
PLAIN TEXT
PHP:
1.
$myVariable = "its' value";
2.
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.
5.
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.
6.
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.

2 comments

Chart Smart said... @ Thursday, December 27, 2007 at 8:45:00 AM GMT

NICE BLOG :)

HAPPY NEW YEAR :)

Unknown said... @ Saturday, August 1, 2009 at 3:24:00 PM GMT+1
This comment has been removed by a blog administrator.

Post a Comment

Please put your comments here. your questions, your suggestions, also what went wrong with me.