PHP Developer Interview Questions

Name the Four Fundamental Principles of OOP?
What is the difference between PUBLIC, PRIVATE AND PROTECTED
  • PUBLIC scope to make that variable/function available from anywhere, other classes and instances of the object.
  • PRIVATE scope when you want your variable/function to be visible in its own class only.
  • PROTECTED scope when you want to make your variable/function visible in all classes that extend current class including the parent class.
  • Manual

Describe the difference from an array and an associative array?
Arrays in PHP are really “maps” not the strict arrays you find in other languages. The main difference is that you define the indexes in the associative array. While when you do not define the indexes are defined by the system starting at 0 and incremented by 1. Manual
Describe the difference between double equals (==) and triple equals (===)
Directly from the manual:
$a == $b Equal TRUE if $a is equal to $b after type juggling.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.

Examples:

//Type comparison (Bool vs Int):
var_dump((false == 0)); //true
var_dump((true === 1)); //false

//Type comparison (String vs Int):
var_dump(("0" == 0)); //true
var_dump(("1" === 1)); //false

//Object comparison
// __toString is not compared
$t1 = new Test();
$t2 = new Test();

var_dump(($t1 == $t2)); //True
var_dump(($t1 === $t2)); //False

$t1->val = "yes";
$t2->val = "yes";

var_dump(($t1 == $t2)); //True
var_dump(($t1 === $t2)); //False

$t1->val = "yes";
$t2->val = "no";

var_dump(($t1 == $t2)); //False
var_dump(($t1 === $t2)); //False
Give an example of using GROUP BY in MySQL
-- Count orders by zip code
SELECT `zip`, COUNT(`zip`)
FROM orders
GROUP BY `zip`;
Explain the difference between WHERE and HAVING in MySQL?
They function similarly, but their uses differ. The WHERE statement is used to filter field values that are in the table. The HAVING statement is used to filter calculated values, like sum().

-- WHERE query
SELECT `order_number`, `state`
FROM orders
WHERE `created_at` > '2014-01-01';
-- Returns all orders created after 2014-01-01

-- HAVING query
SELECT `invoicenumber`, SUM(`amount`) as `total`
FROM invoices
GROUP BY `invoicenumber`
HAVING `total` > 500;
-- Returns all invoices that have a total amount over 500.