Skip to main content

Weak comparisons

PHP 8 introduces severalimportant changes comparedto weak comparisons. Comparisons with == and != now follow stricter rules, so many comparisons must be updated to avoid unexpected behavior.

Key Principles

Follow these fundamental rules when writing PHP 7,8 aimingcompatible code:

Analyze variable types: Always understand what types and values a variable can hold throughout its lifecycle.

Use strict comparisons: Prefer === and !== over == and != to improveavoid languageimplicit consistency,type security,coercion.

Declare types explicitly: Always use type declarations for function/method parameters and codereturn predictability.
Sometypes.

features

Validate havewith beenstrict simplifiedchecks: orUse madestrict stricter,type particularlychecks regardingin conditional statements to prevent unexpected behavior.

Use PHP constants: Always use predefined PHP constants instead of numbers (e.g., UPLOAD_ERR_OK instead of 0).

Enable strict types: Add declare(strict_types=1); at the type system and weak comparisons, which in certain cases may produce different results than in previous versions. These changes can affect the compatibilitytop of existingyour codePHP andfiles requirewhen specialpossible.

attention

Strings

during

Important: theUse upgradeempty() process.instead of comparing with empty string.

❌ DON'T ✅ DO
# Strings (you are sure that the variable is a string!)

Before:
```php
if ($a == '') {
    // ...
}
``` After: ```php
if (empty($a)) {
    // ...
}
``` Before: ```php
if ($a != '') {
    // ...
}
``` After: ```php
if (!empty($a)) {
    // ...
}
``` #

Checkbox valuevalues

Before:

In ```phpPHP 8, comparing numeric strings with numbers produces different results.

❌ DON'T ✅ DO
if ($a == 0) {
    // ...
} elseif ($a == 1) {
    // ...
} else {
    // ...
}
``` After: ```php
if (strval($a) === '0') {
    // ...
} elseif (strval($a) === '1') {
    // ...
} else {
    // ...
}
```

# Picklist value

Before:
```php
if ($a != '') {
	// ...
}
```

After:
```php
if ($a !== '') {
	// ...
}
```

# Permissions (profileGlobalPermission, profileTabsPermission, profileActionsPermission, ...)

Before:
```php
if ($is_admin == true || $profileGlobalPermission[1] == 0 || $profileGlobalPermission[2] == 0 || $module == 'Users') {
	// ...
}
```

After:
```php
if ($is_admin == true || $profileGlobalPermission[1] === 0 || $profileGlobalPermission[2] === 0 || $module == 'Users') {
	// ...
}
```

# PearDatabase

Before:
```php
if ($adb->num_rows($result) == 0) {
	// ...
}
```

After:
```php
if ($adb->num_rows($result) === 0) {
	// ...
}
```

Before:
```php
if ($adb->num_rows($result) != 0) {
	// ...
}
```

After:
```php
if ($adb->num_rows($result) > 0) {
	// ...
}
```

Before:
```php
if ($adb->getAffectedRowCount($result) == 0) {
	// ...
}
```

After:
```php
if ((int) $adb->getAffectedRowCount($result) === 0) {
	// ...
}
```

# $_FILES

Before:
```php
if ($_FILES[$filename_fieldname]['error'] == 0) {
	// ...
}
```

After:
```php
if ($_FILES[$filename_fieldname]['error'] === UPLOAD_ERR_OK) {
	// ...
}
```