devnotes.

Logical and String Operators

Logical operators combine boolean expressions:

$a && $b;   // AND — true if both are true
$a || $b;   // OR — true if either is true
! $a;       // NOT — inverts a boolean
$a and $b;  // word form of &&, lower precedence
$a or $b;   // word form of ||, lower precedence
$a xor $b;  // exclusive or — true if exactly one is true

PHP uses short-circuit evaluation: in $a && $b, if $a is false, $b is never evaluated. This is commonly used defensively:

if ($user && $user->isActive()) { ... } // avoids calling a method on null

String operators:

$greeting = "Hello, " . $name . "!"; // concatenation with .
$greeting .= " Welcome.";             // concatenate and assign

Array operators:

$merged = $arr1 + $arr2; // union — keys from $arr1 win on conflict
$a == $b;  // true if same key/value pairs, any order
$a === $b; // true if same key/value pairs, same order and types

Understanding operator precedence (multiplication before addition, && before ||, etc.) matters for complex expressions — when in doubt, use parentheses to make the intended order explicit and the code more readable.

PHP Logical, String & Array Operators | DevNotes