Php /

PHP 7.4 to 8.2

Probleme beim Wechsel von PHP7.4 zu PHP8.2]

Ich musste gerade mit meinem Provider von PHP7.4 auf PHP8 wechseln ... Dabei habe ich gleich den Schritt bis PHP8.2 gemacht, was leider einige Probleme in meinem Skripen erzeugt hat.

Diese Liste zeigt alle Probleme, die ich hatte und welche Lösungen bei mir zu einer Problemlösung geführt haben

Deprecated function: preg_match(): Passing null to parameter #2 ($subject)

Lösung: Ich ergänze hinter dem zweiten Parameter die Eingabe mit ?? '' und die Funktion läuft wieder.

Lösung gefunden ...
Firstly, two things to bear in mind:

  1. PHP 8.1 deprecates these calls, it does not make them errors. The purpose of deprecation is to give authors advance notice to fix their code, so you and the authors of libraries you use have until PHP 9.0 comes out to fix things. So, don't panic that not everything is fixed right away, and be patient with library maintainers, who will get to this in their own time.
  2. The quick fix in most cases is to use the null coalescing operator to provide a default value as appropriate, so you don't need a long null check around every use. For instance, htmlspecialchars($something) can be replaced with htmlspecialchars($something ?? '')

Next, some options:

  • Depending how many cases you have, you may be able to just fix them manually a few at a time, either adding ?? '' or fixing a logic bug where you weren't expecting a null anyway.
  • Create custom functions like nullable_htmlspecialchars and do a straight-forward find and replace in your code.
  • Create custom namespaced functions like nullableoverride\htmlspecialchars; then in any file where you add use function nullableoverride\htmlspecialchars; that function will be used instead of the built-in one. This has to be added in each file, though, so you may need a tool to automate adding it.
  • Use Rector to automate adding ?? '' to appropriate function calls, so you don't have to edit them all by hand. Unfortunately, there doesn't seem to be a built-in rule for this (yet), so you'd have to learn to write your own.
  • Possibly simpler, depending on your skills, use a regular expression find-and-replace to add the ?? '' to simple cases.

Deprecated: Function utf8_decode() is deprecated


  • raus ... utf8_decode($string);
  • rein ... mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8');

Lösung gefunden ...

Deprecated: Function utf8_encode() is deprecated


  • raus ... utf8_encode($string);
  • rein ... mb_convert_encoding($string, 'UTF-8', mb_list_encodings());

Lösung gefunden ...

Optional parameter $curFilter declared before required parameter $curReg is implicitly treated as a required parameter ...

Lösung: Therefore, your prototype should change from:

public function send(string $to_name = "", string $to_addr, string $from_name, string $from_addr, string $subject = "", bool $reply_to = false): bool to

public function send(string $to_addr, string $from_name, string $from_addr, string $to_name = "", string $subject = "", bool $reply_to = false): bool

Lösung gefunden ...