Undefined_constant:PHP 7升级后出现错误

时间:2020-11-03 09:17:52

标签: php random random-seed

我有这个简单的PHP脚本(authors.php),可以以随机顺序轮流浏览图像列表

<?php 
$random = mt_rand(1,9);
$image_1 = '<img src="/gallery/authors/1.jpg">';
$image_2 = '<img src="/gallery/authors/2.jpg">';
$image_3 = '<img src="/gallery/authors/3.jpg">';
$image_4 = '<img src="/gallery/authors/4.jpg">';
$image_5 = '<img src="/gallery/authors/5.jpg">';
$image_6 = '<img src="/gallery/authors/6.jpg">';
$image_7 = '<img src="/gallery/authors/7.jpg">';
$image_8 = '<img src="/gallery/authors/8.jpg">';
$image_9 = '<img src="/gallery/authors/9.jpg">';
echo ${image_.$random};
?>

在我从PHP 5.6升级到PHP 7.2.34之前,它一直运行良好。

现在我收到此错误。

Use of undefined constant image_ - assumed 'image_' (this will throw an Error in a future version of PHP) in /home/website/public_html/authors.php, line 12

要使此脚本起作用,需要更改什么?

谢谢

1 个答案:

答案 0 :(得分:2)

尝试此方法,而不要排长队长

$random = mt_rand(1,9);
echo "<img src=\"/gallery/authors/{$random}.jpg\">";