skip to Main Content

My php codes work fine with my web pages in PHP 7.0, but after upgrading to PHP 8.0, my php web pages don’t work anymore. Here are the errors and warnings below. I am not a programmer. Could you please help me to fix them? Thanks for considering my request. Thanks again. — Andy

[21-Jun-2023 23:17:27 America/New_York]

PHP Warning: Undefined variable $scrollamount in /homepages/d13xxxxx/htdocs/wish/wish.php on line 541
PHP Warning: Undefined variable $scrollamount in /homepages/d13xxxxx/htdocs/wish/wish.php on line 542
PHP Warning: Undefined variable $message in /homepages/d13xxxxx/htdocs/wish/show.php on line 11


// -- **next lines are 541 and 542**

if($scrollamount<=5){

$scrollamount=$scrollamount+1;

} else {

$scrollamount=1;

}

echo "<marquee scrollamount=$scrollamount delay=0><a href=\"javascript:openChromeslessWindow('show.php?        number=$f[0]','data',310,310,null,null,'查看許願內容', '#000000', '#000000', '#999988', '#808040' ,true,'??','1','#000000')\" ONMOUSEOVER=\"pop('來自".$f[7]."<br>".$f[6]."歲的".$f[1]."<br>於".$f[2]."<br>在這裡許了個願<br>共有".$f[8]."個人看過')\"; ONMOUSEOUT=\"kill()\"><img src=\"images/c".$f[3].".gif\" border=0></a></marquee>";

}
}
?>

.
.

// ... PHP Warning: Undefined variable "$message", see below:

include "config.inc";

$f=file("database/".$_GET['number']);

for($i=0;$i<count($f);$i++){

if($i==8)$f[$i]=$f[$i]+1;

// Next line # is 11 -- PHP Warning: Undefined variable "$message" 
$message=$message.$f[$i];

$ff=explode("n",$f[$i]);

$f[$i]=$ff[0];

}

$fp=fopen("database/".$_GET['number'],"w");

flock($fp,3);

fwrite($fp,$message);

fclose($fp);`
</code>`

(Do not know how to define the variables? I knew it works in PHP 6.0 and PHP 7.0.)

2

Answers


  1. you should declare variable "$scrollamount" and "$message" before loop for
    .Example:

    $message = "";
    for($i=0;$i<count($f);$i++){
    
        if($i==8){
           $f[$i]=$f[$i]+1;        
    
            $message=$message.$f[$i];
            $ff=explode("n",$f[$i]);
            $f[$i]=$ff[0];
    }
    

    }

    Login or Signup to reply.
  2. For $scrollamount you don’t have a previous value so you could use the coalesce operator ?? and if you want to assign a default value you could use something like this:

    $scrollamout = ($scrollamout ?? 0) <= 5 ? ($scrollamout ?? 0) + 1 : 1;
    

    And for $message the same:

    $message = ($message ?? '') . $f[$i];
    

    That way, you are undefined-safe error because you will have a default value in case there’s no previous value.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search