Weak comparisons
PHP 8 introduces several changes compared to PHP 7, aiming to improve language consistency, security, and code predictability.
Some features have been simplified or made stricter, particularly regarding the type system and weak comparisons, which in certain cases may produce different results than in previous versions. These changes can affect the compatibility of existing code and require special attention during the upgrade process.
# 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 value
Before:
```php
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) {
// ...
}
```