Using AND/OR in if else PHP statement

Using AND/OR in if else PHP statement

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Logical Operators in PHP Conditionals: AND vs OR

When constructing conditional logic in PHP, the choice between AND and OR fundamentally alters which rows of data your application will process. In the context of the example provided, where you are updating a database record based on $status or $pRent, selecting the wrong operator can lead to unintended side effects, such as modifying records that should remain untouched.

The proposed solution using AND executes the update only if both conditions are simultaneously true. In your specific query:

if ($status === 'clear' AND $pRent == 0) {
    // Update logic here
}

This block runs exclusively when $status is strictly 'clear' and $pRent equals 0. If $status is 'clear' but $pRent is 5, the condition evaluates to false, and no SQL command is sent. This is appropriate for strict filtering where multiple criteria must be met before an action occurs. Conversely, if you used OR:

if ($status === 'clear' OR $pRent == 0) {
    // Update logic here
}

The condition becomes true if either $status is 'clear' or $pRent is 0. This might seem like a good way to handle multiple states, but it introduces a significant logical pitfall known as the "always true" trap. Since almost every database record will eventually have a non-zero rent value or a cleared status, relying on loose equality checks with OR can accidentally trigger updates on records you didn't intend to touch.

A critical observation regarding your source code snippet is the use of single assignment operators (=) instead of strict comparison operators (===). In PHP, $status = 'clear' attempts to assign the string 'clear' to the variable $status rather than comparing them. While this syntax often works in loose contexts because the result of the assignment is truthy, it introduces a silent bug where you are modifying your variable inside your condition. If $status was previously null, the line $status = 'clear' changes it, potentially altering the state of your application logic before the next step runs. For robust code, always use === for comparisons to ensure type safety and prevent accidental assignment.

Furthermore, the source material demonstrates a common security vulnerability: SQL Injection. The query string directly interpolates variables like $id, $status, and $pRent into the SQL command without sanitization. Even if you fix your logical operators, this code remains insecure. An attacker could manipulate the input to bypass your AND or OR conditions entirely and execute arbitrary database commands.

To resolve this safely, you must use prepared statements. If you are sticking with raw PHP logic, wrap values in quotes and sanitize them, but moving to a framework like Laravel is the industry standard for handling these concerns automatically. Laravel provides elegant syntax using its query builder or Eloquent ORM that handles binding parameters securely by default, eliminating the risk of SQL injection regardless of how complex your AND or OR chains become.

// Secure example using raw queries with bindings
$stmt = DB::table('rent')
    ->where('id', $id)
    ->update([
        'dNo' => $id, 
        'status' => 'clear', 
        'colour' => '#3C0' 
    ]);

// Or using Eloquent
$rent = Rent::findOrFail($id);
$rent->update(['status' => 'clear', 'colour' => '#3C0']);

The decision between AND and OR ultimately depends on your business logic requirements. If clearing a record requires specific prerequisites, use AND. If the action should occur if any one of several states is present, use OR, but be extremely careful to ensure that the combination of conditions doesn't create an overly broad scope that modifies unintended data. Always prioritize secure coding practices by using strict comparisons and prepared statements over direct string interpolation.

Our network