Oct 29, 2011

A Good PHP Developer Can Answer This | PHP Test

PHP developers go through 3 stages in their life

  • Beginner

  • Good

  • Best


A beginner PHP coder is some one who just started making some PHP projects in CMS like Wordpress, Joomla, Magento and other PHP based CMS.

A good PHP coder completed or knows all PHP based CMS and is about to finish PHP Frameworks like Yii, Cake, Symphony, CI and others.

A best PHP coder is a complete package of Beginner and Good, that person needs no questions cause he might be ready with all the answers.

Now its time to rate yourself in PHP, I googled and found some questions which only a Good coder can answer.

Q1: What is T_PAAMAYIM_NEKUDOTAYIM?
A: Its the scope resolution operator (double colon ::). This is valid for PHP 4 and later only.

Q2: What is the cause of this warning: 'Warning: Cannot modify header information - headers already sent', and what is a good practice to prevent it?
A: Reason: body data was sent, causing headers to be sent too. This normally occurs when white space is sent accidentally.

Q3: What's wrong in this query: "SELECT * FROM table WHERE id = $_POST[ 'id' ]"?
A: This is the worst practice to select some row or fetch data, use specific row instead of *. Use PDO prepared statements to avoid SQL Injections.

Q4: What is wrong with this if statement: if( !strpos( $thread, $needle ) ...?
A: strpos returns the index position of needle, it could return false if its at 0 position. Instead give a if else check if( false !== strpos( $thread, $needle )...

Q5: What is the preferred way to write this if statement, and why?
if( 5 == $someVar )
or if( $someVar == 5 )
A: The former, it prevents accidental assignments of 5 to $somevar if you forgot two == signs(if( $someVar = 5 ))

Q6: In the below code, whats the value of $a and $b after the function call and why?

[php]
function doSomething( &$arg )
{
$return = $arg;
$arg += 1;
return $return;
}

$a = 3;
$b = doSomething( $a );
[/php]

A: $a is 4 and $b is 3. The former because $arg is passed by reference, the latter because the return value of the function is a copy of (not a reference to) the initial value of the argument.

Q7: What is the difference between public, protected and private in a class definition?
A: public makes a class member available to "everyone", protected makes the class member available to only itself and derived classes, private makes the class member only available to the class itself.

Q8: What is wrong with this code:

[php]
class SomeClass
{
protected $_someMember;

public function __construct()
{
$this->_someMember = 1;
}

public static function getSomethingStatic()
{
return $this->_someMember * 5; // here's the catch
}
}

[/php]

A: Static methods don't have access to $this, because static methods can be executed without instantiating a class.

Q9: What is the difference between an interface and an abstract class?
A: An interface defines a contract between an implementing class is and an object that calls the interface. An abstract class pre-defines certain behaviour for classes that will extend it. To a certain degree this can also be considered a contract, since it garantuees certain methods to exist.

Q10: Which one is correct?: $array[name] or $array['name']?
A: Both of them will output the value, but only the quoted form is correct. To check this define(name,0); and see the bugs flying in the website.

Now how much did you score out of 10, comment your score below, and if you have any more questions then let me know after all sharing is caring ;)

1 comment :