4

I'm using TCPDF plugin to generate the PDF in PHP 7. The same code is working fine in the lower version PHP 5 but when I run this same code in the PHP 7 it's giving the below error message.

A PHP Error was encountered
Severity: 8192

Message: The each() function is deprecated. This message will be suppressed on further calls

Filename: tcpdf/tcpdf.php

Line Number: 16542
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nashir
  • 254
  • 1
  • 5
  • 9
  • Does this answer your question? [How can I update code that uses the deprecated each() function?](https://stackoverflow.com/questions/46492621/how-can-i-update-code-that-uses-the-deprecated-each-function) – Dharman Jun 23 '21 at 20:08

3 Answers3

10

Edit in File: \FPDI\fpdi.php the Line 567:

//while (list($k, $v) = each($value[1])) {

in Code: foreach ($value[1] AS $k => $v) {

And edit in File: \tcpdf\tcpdf.php the Line 16543:

//while (list($id, $name) = each($attr_array[1])) {

in Code: foreach($attr_array[1] as $id => $name) {

Sebastian
  • 883
  • 11
  • 32
  • 3
    This does the trick! There are 3 spots in tcpdf.php using tecnickcom/tcpdf version 6.2.26 – Loren Apr 17 '19 at 23:57
5

Note to anybody that finds this...the latest version of TCPDF has this fixed...so if you simply do an update you should be ok: https://github.com/tecnickcom/TCPDF

user2662680
  • 677
  • 8
  • 16
1

According to php:

This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.

http://php.net/manual/en/function.each.php

As I recall I also have a "legacy" script with each. Rather than modifying it I just turned off depreciated error warnings (for now).

index.php

switch (ENVIRONMENT) {
    case 'development':
        error_reporting(~E_DEPRECATED);
        ini_set('display_errors', 1);
        break;
    case 'testing':
    case 'production':
        ini_set('display_errors', 0);
        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
        break;
    default:
        header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
        echo 'The application environment is not set correctly.';
        exit(1); // EXIT_ERROR
}

You could update the library as I believe it is still in development or if that isn't the case you can also modify the code replacing each with a proper foreach loop where required:

How to resolve this deprecated function each php

Alex
  • 9,215
  • 8
  • 39
  • 82