Magic Constants & Operators
Magic constants are built-in placeholders that change depending on where they appear: __LINE__, __FILE__, __DIR__, __FUNCTION__, __CLASS__, __METHOD__, __NAMESPACE__.
Magic Constants
Magic constants are built-in placeholders that change depending on where they appear: __LINE__, __FILE__, __DIR__, __FUNCTION__, __CLASS__, __METHOD__, __NAMESPACE__.
Real-life example: Magic constants are like automatic name tags at a conference — they always show your current booth, room, or session.
<?php
function showDebugInfo() {
echo "Function: " . __FUNCTION__ . "\n";
echo "Line: " . __LINE__ . "\n";
echo "File: " . __FILE__;
}
showDebugInfo();
?>Operators — arithmetic and assignment
Use +=, -=, *=, /= for shorthand assignment. The concatenation operator . joins strings.
Real-life example: += is adding more coins to a piggy bank without counting the old total first.
<?php
$score = 10;
$score += 5; // 15
$greeting = "Hello";
$greeting .= ", Rishtaara!"; // "Hello, Rishtaara!"
echo $greeting;
?>Comparison and logical operators
Compare with ==, ===, !=, !==, <, >, <=, >=. Combine conditions with &&, ||, and !. Prefer === to avoid type surprises.
Real-life example: === checks both the gift and the wrapping paper match. == only checks if the gift looks similar.
<?php
$age = 18;
$hasId = true;
if ($age >= 18 && $hasId) {
echo "Entry allowed";
}
// Strict vs loose
var_dump(0 == ""); // true (loose)
var_dump(0 === ""); // false (strict)
?>