Operators

Operators in PHP let you perform actions on values and variables — basically, they’re what make your data do things.
PHP groups them into a few types: Arithmetic, Assignment, Comparison, Increment/Decrement, Logical, String, Array, and Conditional operators.


Arithmetic Operators

Used with numbers to do math: add, subtract, divide, etc.

$clicks_today = 120;
$clicks_yesterday = 85;

$total = $clicks_today + $clicks_yesterday;  // Addition
$diff = $clicks_today - $clicks_yesterday;   // Subtraction
$ratio = $clicks_today / $clicks_yesterday;  // Division
$growth = $clicks_today % $clicks_yesterday; // Modulus (remainder)

Or, if you’re feeling fancy:

$power = 2 ** 3; // 8 (Exponentiation)

Assignment Operators

These let you assign or update variable values.

$total_clicks = 100;
$total_clicks += 20; // adds 20 (same as $total_clicks = $total_clicks + 20)
$total_clicks -= 5;  // subtracts 5
$total_clicks *= 2;  // multiplies by 2
$total_clicks /= 3;  // divides by 3

You’ll see these a lot in loops or when calculating totals in plugin data.


Comparison Operators

Used when you want to compare two things — equality, greater than, etc.

$plan = 'Pro';
$user_plan = 'Pro';

var_dump($plan == $user_plan);   // true
var_dump($plan === $user_plan);  // true (same value and same type)
var_dump($plan != 'Basic');      // true
var_dump(10 < 5);                // false
var_dump(10 >= 5);               // true

And the spaceship operator (<=>), new since PHP 7, is great for sorting:

echo 10 <=> 15; // -1 (less than)
echo 15 <=> 15; // 0 (equal)
echo 20 <=> 15; // 1 (greater than)

Increment / Decrement Operators

Increase or decrease a value by one.

$counter = 5;

echo ++$counter; // 6 (pre-increment)
echo $counter++; // 6 (then becomes 7)
echo --$counter; // 6 (pre-decrement)
echo $counter--; // 6 (then becomes 5)

Useful when looping through MemberPress transactions or iterating through links.


Logical Operators

Combine conditions — for example, checking if multiple things are true.

$is_logged_in = true;
$has_active_membership = true;

if ($is_logged_in && $has_active_membership) {
  echo "Access granted!";
}

List of logical operators:

  • and, && → both must be true
  • or, || → at least one must be true
  • xor → only one can be true, not both
  • ! → negation (not)

String Operators

Two special ones for working with text.

$name = "MemberPress";
$message = "Welcome to " . $name; // Concatenation
$message .= " support!";          // Appends more text

Now $message is "Welcome to MemberPress support!".


Array Operators

Used to combine or compare arrays.

$a = ["plugin" => "Pretty Links", "active" => true];
$b = ["version" => "4.0.0"];

$result = $a + $b; // Union
var_dump($result);

Comparison examples:

$x = ["plugin" => "Pretty Links"];
$y = ["plugin" => "Pretty Links"];

var_dump($x == $y);  // true (same key/values)
var_dump($x === $y); // true (same order and type)

Conditional Assignment Operators

These help you write shorter conditional checks.

Ternary operator (?:):

$user_plan = "Pro";
$message = ($user_plan === "Pro") ? "Welcome back, Pro user!" : "Upgrade for more features.";

Null coalescing operator (??):

$username = $_GET['user'] ?? 'Guest';

This one is super common when handling optional data safely.