0

I use PHPgraphlib for many years and like that it generates a reliable image instead of some fancy Javascript-stuff. Now since update to PHP7, it renders a log-error on line 926 of the original code:

PHP Warning: count(): Parameter must be an array or object that implements Countable in ./phpgraphlib.php on line 926, referrer ...

From line 926:

    protected function displayErrors() 
    {
        if (count($this->error) > 0) {
            $lineHeight = 12;
            $errorColor = imagecolorallocate($this->image, 0, 0, 0);
            $errorBackColor = imagecolorallocate($this->image, 255, 204, 0);
            imagefilledrectangle($this->image, 0, 0, $this->width - 1, 2 * $lineHeight,  $errorBackColor);
            imagestring($this->image, 3, 2, 0, "!!----- PHPGraphLib Error -----!!",  $errorColor);
            foreach($this->error as $key => $errorText) {
                imagefilledrectangle($this->image, 0, ($key * $lineHeight) + $lineHeight, $this->width - 1, ($key * $lineHeight) + 2 * $lineHeight,  $errorBackColor);  
                imagestring($this->image, 2, 2, ($key * $lineHeight) + $lineHeight, "[". ($key + 1) . "] ". $errorText,  $errorColor);  
            }
            $errorOutlineColor = imagecolorallocate($this->image, 255, 0, 0);
            imagerectangle($this->image, 0, 0, $this->width-1,($key * $lineHeight) + 2 * $lineHeight,  $errorOutlineColor);     
        }
    }

I tried to uncomment it, but it throws another error on line 271

        //display errors
        $this->displayErrors();

I do quite a bit with PHP and MySQL, but this exceeds my amateuer know-how. Any help welcome! Aside this error, phpgraphlib keeps working like a charm. And I use the error, as I couldn't find anything searching for this error on phpgraphlib. References to where answered sure welcome.

Thanks a million!

Juergen
  • 11
  • 2
  • PHPgraphlib hasn't been updated since 2016 and most likely isn't compatible with PHP7 without a lot of modifications. `$this->error` isn't an array (possibly it's null or an empty string), so it can't be used with `count()`. Look to see where it's defined and how it's defined. *Edit* Looking at the [source](https://github.com/jeroenst/phpgraphlib/blob/master/phpgraphlib.php), it doesn't have a default value, so setting it to `[]` will work. But that is probably just the start of your issues. – aynber Mar 31 '22 at 14:11
  • Aside not being updated (not even the forks), this is the only warning I get with PHP7, so why shelf a running system that does what I need? I tried to set it to nothing, but then it shows the (empty) error in the graph. i.e. if (!isset($this->error)) { $this->error[] = ""; } to line 269. So it seems if it is set, it shows. If not, it causes a warning in the log. I am amateur, not a programmer I'm afraid, I can use and adjust simple things, but $this- – Juergen Mar 31 '22 at 14:59
  • No, don't try to add an empty value to the array. `protected $error;` needs to be changed to `protected $error = [];` – aynber Mar 31 '22 at 15:01

1 Answers1

0

As commented by @aynber - adding =[] to protected $error; in line 137 did the trick. New line reads

protected $error = [];

Juergen
  • 11
  • 2