php 7.4 and php 8.1



When I run my script in php 7.4, it is smooth and perfect.
but same gives the error when I run in php 8.1

Web page says:
Sorry, Image Validation Failed !

Error log says:
PHP Warning: Undefined variable $msg in /home/xxx.php on line nn

Any suggestion.
I also cannot upgrade my PHP version on my site In Cpanel its showing it's 8.1 but in Wordpress its showing 6.4
try to change the default version of php for the account, sometimes you will need to restart the http server. Are you using apache, nginx iis or other?
The issue in your code is that you are missing a concatenation operator (.) between $CharList and rand(0, $Max) when appending characters to the $String variable. Additionally, PHP 7.4 introduced stricter error reporting, which may be why you are now encountering an error. To fix the code and make it compatible with PHP 7.4, you should update the line inside the for loop as follows:
$String .= $CharList . $CharList[rand(0, $Max)];

Here's the updated code:
function random_string($Ncharacters) {
static $CharList = "QWERTYUPKJHGFDSAZXCVBNMqwertyupkjhgfdsazxcvbnm";
$String = "";
$Max = strlen($CharList) - 1;
for ($i = 0; $i < $Ncharacters; $i++) {
$String .= $CharList . $CharList[rand(0, $Max)];
return $String;

With this change, the code should work correctly in PHP 7.4 and concatenate random characters to the $String variable.