Nashir
Nashir

Reputation: 284

TCPDF errors on PHP 7

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

Upvotes: 4

Views: 14899

Answers (3)

user2662680
user2662680

Reputation: 706

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

Upvotes: 5

Sebastian
Sebastian

Reputation: 929

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) {

Upvotes: 10

Alex
Alex

Reputation: 9265

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

Upvotes: 1

Related Questions