phpLiteAdmin / pla

Official github clone of the phpLiteAdmin repository
https://www.phpliteadmin.org/
173 stars 36 forks source link

PHP Warning: sizeof on line 1130 #10

Closed halojoy closed 6 years ago

halojoy commented 6 years ago

In trunk index.php PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable in C:\Apache24\htdocs\index.php on line 1130

Does not give an error, but gives a Warning.

crazy4chrissi commented 6 years ago

Hi. Thanks for reporting. I guess you are using PHP 7.2?

crazy4chrissi commented 6 years ago

Yeah, the use of sizeof here is completely wrong and just works by chance :D

This is the line: $query = substr($query, 0, sizeof($query)-3); This seems to be written by a C-programmer who thinks sizeof() returns the number of bytes of the string. This line should cut off two characters. Don't know why -3 is given, probably because of the null byte in C strings (??) or because -2 just didn't work...

What it actually does: In PHP, sizeof() is an alias for count() and only works for arrays (and Countable objects). For strings, is always returns 1. Then this length parameter is 1-3=-2. With negative length parameters, substr cuts of as many characters, so it will cut off 2 characters. So it works as it should.

But it would be much simpler and would not produce a warning in PHP 7.2 if it would be just: $query = substr($query, 0, -2); Of course strlen($query)-2 would work as well, but it's not necessary.

I will correct it and search for all occurences of sizeof to check if the same mistake is done somewhere else...

Thanks again for reporting.