PHP Basic Tutorial
MySQL Connection
PHP Advanced
PHP OOP
In PHP, == and === are comparison operators, but they behave differently:
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
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:
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
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
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
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
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 'file.php'; // Warning if file not found echo "Hello World"; // This will still execute
require 'file.php'; // Fatal error if file not found echo "Hello World"; // This will NOT execute if file is missing
include_once 'file.php'; include_once 'file.php'; // Ignored the second time
require_once 'file.php'; require_once 'file.php'; // Ignored the second time
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.
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.
To return a reference to a variable (used rarely and cautiously).
function &getValue(&$array) { return $array['key']; }
Modify array items directly inside a loop.
$colors = ['red', 'green', 'blue']; foreach ($colors as &$color) { $color = strtoupper($color); } print_r($colors); // ['RED', 'GREEN', 'BLUE']