Advertisement

Google Ad Slot: content-top

PHP Interview Question 1


1.What are the differences between == and === in PHP?

In PHP, == and === are comparison operators, but they behave differently:


✅ == (Equality Operator)

  • Compares values only, after type juggling (type conversion if necessary).
  • It does not check the data type.


✅ === (Identity Operator)

  • Compares both value and type.
  • It returns true only if both the value and the type are the same.
var_dump(0 == false);     // true
var_dump(0 === false);    // false
var_dump(null == false);  // false
var_dump(null === false); // false
var_dump("0" == false);   // true (string "0" is falsy)
var_dump("0" === false);  // false

2.How does PHP handle variable scope?

PHP handles variable scope through a set of rules that determine where variables can be accessed or modified. There are four main types of scope in PHP:


Local Scope

Variables declared inside a function are only accessible within that function.

function test() {
    $x = 5; // local scope
    echo $x;
}
test();     // Output: 5
// echo $x; // Error: Undefined variable


Global Scope

Variables declared outside of functions are in the global scope, and not accessible inside functions unless declared as global.

$x = 10;
function showX() {
    global $x;     // brings global $x into function scope
    echo $x;
}
showX(); // Output: 10


Static Scope

Variables declared as static inside a function retain their value between function calls.

function counter() {
    static $count = 0;
    $count++;
    echo $count . "\n";
}
counter(); // Output: 1
counter(); // Output: 2


Superglobals

PHP provides built-in superglobal variables that are accessible everywhere, including inside functions or classes, without needing global.

Examples: $_GET, $_POST, $_SESSION, $_SERVER, $_FILES, $_COOKIE, $_ENV, $_REQUEST, $GLOBALS

$GLOBALS['x'] = 100;
function printGlobal() {
    echo $GLOBALS['x'];
}
printGlobal(); // Output: 100

3. What is the difference between include, require, include_once, and require_once?

In PHP, the include, require, include_once, and require_once statements are used to insert the content of one PHP file into another. Here's the difference between them:


include

  • Includes and evaluates the specified file.
  • If the file is not found, it throws a warning but the script continues executing.
include 'file.php'; // Warning if file not found
echo "Hello World"; // This will still execute


require

  • Also includes and evaluates the specified file.
  • If the file is not found, it throws a fatal error and the script stops execution.
require 'file.php'; // Fatal error if file not found
echo "Hello World"; // This will NOT execute if file is missing


include_once

  • Same as include, but prevents including the same file more than once.
  • Useful for including function or class definitions only once to avoid redeclaration errors.
include_once 'file.php';
include_once 'file.php'; // Ignored the second time


require_once

  • Same as require, but also prevents multiple inclusions.
  • Safer when the file is essential and should only load once.
require_once 'file.php';
require_once 'file.php'; // Ignored the second time



4.Explain references in PHP (&$var). When and why would you use them?

References in PHP (&$var) – Explanation

In PHP, a reference allows two variables to point to the same memory location, so changing one will change the other. It is done using the & (ampersand) symbol.

$a = "Hello";
$b = &$a; // $b is now a reference to $a
$b = "World";
echo $a; // Outputs: World


Explanation:

Changing $b also changes $a, because both reference the same value.


5.When and Why Use References in PHP

Function Arguments (Pass by Reference)

To allow a function to modify the original variable passed to it.

function addOne(&$number) {
    $number += 1;
}
$x = 5;
addOne($x);
echo $x; // Outputs: 6

✅ Useful for performance (e.g., large arrays) or when the function needs to modify the input.

 

Function Returns (Return by Reference)

To return a reference to a variable (used rarely and cautiously).

function &getValue(&$array) {
    return $array['key'];
}


Looping by Reference

Modify array items directly inside a loop.

$colors = ['red', 'green', 'blue'];
foreach ($colors as &$color) {
    $color = strtoupper($color);
}
print_r($colors); // ['RED', 'GREEN', 'BLUE']


⚠️ Important Notes / Gotchas

  • Always unset reference variables (unset($ref)) after use in loops to avoid unexpected behavior.
  • Overusing references can make code harder to read and debug.
  • Avoid references with immutable values like numbers unless truly needed.