scriptcase.net

PHP 8.1 compability

From release 9.8 and on, Scriptcase is also compatible with PHP 8.1.


Deprecated and/or altered functions in PHP 8.1

From of version 9.8 and on, Scriptcase and the generated applications are compatible with PHP 8.1.

With this changes, some of customized the code by users used in events, functions, methods and libraries (internal or external) of your applications may stop working, due to deprecated or removed functions in the latest versions of PHP. It is the developer's responsibility to correct errors resulting from this incompatibility in codes used in events or libraries.

Below, you can find some functions that will no longer work or have changed in PHP 8.1

1 - Nested ternary operators without explicit parenthes

Assignment conditional expressions, used in a nested way, are no longer allowed.

Before PHP 8:
$result = $aa != "" ? $xxx : isset($bb) && $bb != "" ?  $zzz : "";
From PHP 8 and on:
if ($aa != "") {
	$result = $xxx;
} elseif (isset($bb) && $bb != "") {
	$result = $zzz;
} else {
	$result = "";
}

2 - Accessing array and string values using curly braces

String and array value access using curly brace syntax is deprecated. Use $var[$idx] instead of $var{$idx}.

In PHP 8.1 using brace{} to retrieve values from the array will cause an error.

I.e:

Before PHP 8:
$array = [1, 2];
echo $array[1]; // prints 2
echo $array{1}; // also prints 2
 
$string = "foo";
echo $string[0]; // prints "f"
echo $string{0}; // also prints "f"
From PHP 8 and on:
$array = [1, 2];
echo $array[1]; // prints 2
echo $array{1}; // prints the error: Deprecated: Array and string offset access syntax with curly braces is deprecated...
 
$string = "foo";
echo $string[0]; // prints "f"
echo $string{0}; // prints the error: Deprecated: Array and string offset access syntax with curly braces is deprecated...

3 - Mandatory parameters after optional parameters are discontinued

As of PHP 8, it is no longer allowed to declare a function or method by adding a mandatory parameter after optional parameters.

This means that the following function declaration will result in a Notice:
function foo($param_optional = null, $param_required) {
//           ^^ optional parameter , ^^ required parameter
}
Example of ‘Notice’ generated by PHP:
Deprecated: Required parameter $param_required follows optional parameter $param_optional in ... on line ...

4 - Arithmetic operations with non-numeric strings

Until PHP 7 it was possible to perform mathematical operations with variables containing non-numeric strings, which in this situation was equivalent to zero.
In PHP 8.1 this type of calculation started to show an error.

Before PHP 8, the code below would not result in an error:
$b = "";
$a = $b + 2;
From PHP 8 and on, it is necessary to test if the value of $b is a numeric string or a number, before performing the operation:

I.e 1:
$a = is_numeric($b) ? $b + 2 : 2;
I.e 2:
if (!is_numeric($b)) {
	$b = 0;
}

$a = $b + 2;

5 - Non-existent variables and indexes

Until PHP 7 there was a certain tolerance regarding references to undefined variables or indexes, generating only a Notice.

In PHP 8, any reference to a non-existent variable or array index will cause a Warning.

So, if your PHP is enabled to display Warnings, you will encounter errors for that context.

To solve it, it is necessary to test if a variable or array index exists, before using it in some operation.

I.e:
$a = isset($b) ? $b + 2 : 2;

6 - ‘each()’ Function

Returns the current key/value pair of an array and advances its cursor.
This function was removed in PHP 8, if you are using it in your applications, the code must be changed before publishing in an environment with PHP 8 or higher.

Some substitution options:
foreach()
I.e:
$result = "";
$tab = array(0=>array('tp'=>1,'vl'=>'aaa'),
	    1=>array('tp'=>2,'vl'=>'bbb'),
	    2=>array('tp'=>3,'vl'=>'ccc'));
Before PHP 8:
while (list ($key, $val) = each($tab)) {
	$result .= $tab[$key]["vl"];
}
From PHP 8, using foreach():
foreach ($tab as $key => $val) {
	$result .= $tab[$key]["vl"];
}

7 - array_key_exists() Function

Checks if a key or index exists in an array.
Its use in objects is deprecated, instead they must be used isset()   or   property_exists().

Before PHP 8:
if (array_key_exists('index', $array1)) {
	// codigo
}
From PHP 8 and on:
if (isset($array1['index'])) {
	// codigo
}

8 - money_format() Function

Provide number format as a currency string.
This function has been removed from PHP 8 and on, it can be replaced by the intl NumberFormatter functionality.

Before PHP 8:
echo money_format("%.2n", 1234.56);
From PHP 8 and on:
$currencyObject = new NumberFormatter("pt-BR", NumberFormatter::CURRENCY);
echo $currencyObject->format(1234.56);

9 - mb_strrpos() Function

Finds the position of the last occurrence of a value in a string.
Passing the encoding as the third parameter to the function is deprecated.

Instead, pass an offset 0 and the encoding as the fourth parameter:

Before PHP 8:
$string = 'O rato roeu a roupa';
echo mb_strrpos($string, 'roeu', 'UTF-8');
From PHP 8 and on:
$string = 'O rato roeu a roupa';
echo mb_strrpos($string, 'roeu', 0, 'UTF-8');

Full list of PHP changes

To have the complete list of changes in PHP follow the links according to the PHP version.