If statements
When you write code, you’ll often want your script to “decide” what to do depending on a condition. In PHP, that’s where conditional statements come in.
They let your code take different paths — for example, maybe you only want to show a message if a MemberPress user has an active subscription, or run a function if a Pretty Links redirect type matches a certain setting.
The if Statement
The if statement is the simplest form. It runs a block of code only if a condition is true.
if ($is_active) {
echo "User has an active MemberPress subscription.";
}
You can think of it like asking, “Is this true? If so, do this.”
Comparison Operators
Most if statements compare two values. That’s where comparison operators come in:
| Operator | Meaning | Example |
|---|---|---|
== | Equal | $status == 'active' |
=== | Identical (same value and type) | $count === 5 |
!= or <> | Not equal | $link_type != '307' |
!== | Not identical | $user_id !== '123' |
> | Greater than | $visits > 100 |
< | Less than | $clicks < 50 |
>= | Greater or equal | $total >= 0 |
<= | Less or equal | $level <= 3 |
Example:
$clicks = 75;
if ($clicks > 50) {
echo "This Pretty Links URL is performing well!";
}
Logical Operators
You can also combine multiple conditions using logical operators like && (and), || (or), and ! (not).
$user_role = 'admin';
$has_license = true;
if ($user_role == 'admin' && $has_license) {
echo "Access granted to MemberPress settings.";
}
You can stack as many conditions as you need:
if ($a == 2 || $a == 3 || $a == 4) {
echo "Value is between 2 and 4.";
}
The if...else Statement
If you want your script to do one thing if true, and something else if false, you’ll use if...else.
$t = date("H");
if ($t < 20) {
echo "Good day!";
} else {
echo "Good night!";
}
Or, applied to something more familiar:
if ($subscription_active) {
echo "Welcome back!";
} else {
echo "Your MemberPress subscription has expired.";
}
The if...elseif...else Statement
When there are multiple possible outcomes, use elseif blocks.
$t = date("H");
if ($t < 10) {
echo "Good morning!";
} elseif ($t < 20) {
echo "Good day!";
} else {
echo "Good night!";
}
A practical spin:
$status = 'paused';
if ($status == 'active') {
echo "User can access content.";
} elseif ($status == 'paused') {
echo "User needs to renew soon.";
} else {
echo "User has no access.";
}
Shorthand (Ternary) Conditionals
If you want to keep things compact, you can write conditionals on a single line.
One-line if:
if ($clicks > 100) $msg = "Nice traffic!";
Ternary operator (? :):
$msg = ($clicks > 100) ? "Nice traffic!" : "Needs more promotion.";
echo $msg;
This is super common for quick checks in templates or short functions.
Nested Ifs
You can place an if inside another if — useful when conditions depend on previous ones.
if ($member) {
echo "User found.";
if ($member->is_active) {
echo " Subscription is active.";
} else {
echo " Subscription is inactive.";
}
}