Welcome Guest, Not a member yet? Register   Sign In
Uncaught ErrorException: strtolower()

I have an application developed with CI 4.1.4 in localhost which is working well there.
Then, I uploaded the application on a web server, Siteground with php 8.1.0, and there, I got an error message such as:

ErrorException #1
Uncaught ErrorException: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /home/customer/www/simoned113.sg-host.com/app/system/Validation/FormatRules.php:253

The problem is here: SYSTEMPATH/Validation/FormatRules.php at line 253

Here is the code:

PHP Code:
public function valid_ip(?string $ip null, ?string $which null): bool
249        if (empty($ip)) {
250            return false;
251        }
switch (strtolower($which)) {
254            case 'ipv4':
255                $which FILTER_FLAG_IPV4;
256                break;
case 'ipv6':
259                $which FILTER_FLAG_IPV6;
260                break; 

I read in some blog article to replace the null value with '' quotes, but as I am having this error in a system file,
I'm afraid to have in the future some other errors, if I make some changes.

Please any ideas?

Thank yo in advance

(This post was last modified: 11-08-2021, 03:50 AM by kenjis.)

Use PHP 8.0.

CI4 does not support PHP 8.1 yet.
See https://forum.codeigniter.com/thread-80413.html

That is the solution! Thank you so much!

It is a Kludge and not a solution.

PHP newer versions are gradually changing variable type warnings to errors. The errors can be trapped by adding declare(strict_types=1);

The situation will not improve with later versions in fact will become worse.

Where should I put this line: require(strict_types=1) ?
On the top of the BaseController?

(This post was last modified: 11-08-2021, 04:59 PM by John_Betong.)

The declaration should be at the very beginning of the file and if not the an error will be displayed. Give it a try and notice the error message.

Unlike error_reporting(); and ini_set(‘variable’, ‘parameter’); apply to all code following the statements including include files. Strict_types **ONLY** applies to the containing file. This is cunning and essential to ensure legacy included scripts are not affected.

Unfortunately using strict_types in your file will not solve the error because the valid_ip(…); function should only accept strings and NULL is not a string. The function incorrectly accepts NULL as a string and should be replaced with an empty string.

FormatRules.php file should be rewritten forcing strict_types and the function also rewritten to only accept strings.

When using strict_types on your own files, notice since PHP 7.0 all PHP native functions now throw errors when incompatible parameters are passed. Try ini_set(‘display_errors’, true); and notice the errors.

Unfortunately the powers to be prefer using PHP Unit Testing which fails to highlight these strict_type incompatibilities. PHP Unit Testing still passes orange parameters when only apples are valid Smile

I wholeheartedly agree with PHP’s decision to comply to strict_types because once adopted, obscure errors are drastically reduced. Initially numerous errors require small changes for successful validation but results in having more robust code… which will not fail in future PHP versions.

I sincerely wish the developer team would try using strict_types and correct errors which PHP Unit Testing misses. Perhaps strict_types would eliminate the necessity for PHP Unit Testing.

Trust in PHP instead of Third Party applications… because PHP has been refining their code since “June 8, 1995; 26 years ago” Smile

Thank you for your exhaustive and lightening explanation!

Theme © iAndrew 2016 - Forum software by © MyBB