Questions 1 - 11
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.
7. 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:
apd_set_pprof_trace();
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
8. 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:
PLAIN TEXT
PHP:
1. if (strpos("abc", "a") == true)
2. {
3. // this does not get hit, since "a" is in the 0 index position, it returns false.
4. }
5.
6. if (strpos("abc", "a") === true)
7. {
8. // this does get hit as the === ensures this is treated as non-boolean.
9. }
9. How would you declare a class named “myclass” with no methods or properties?
PLAIN TEXT
PHP:
1. class myclass
2. {
3. }
10. How would you create an object, which is an instance of “myclass”?
PLAIN TEXT
PHP:
1. $obj = new myclass();
It doesn't get any easier than this.
11. How do you access and set properties of a class from within the class?
You use the $this->PropertyName syntax.
PLAIN TEXT
PHP:
1. class myclass
2. {
3. private $propertyName;
4.
5. public function __construct()
6. {
7. $this->propertyName = "value";
8. }
9. }
2 comments:
Thanks for the help. Here is a link to help for GATE exams: http://thegateacademy.com/gate-free-test/
Thanks for the help. Here is a link to help for GATE exams: http://thegateacademy.com/gate-free-test/
Post a Comment