Skip to content
Advertisement

Undefined_constant: Error after PHP 7 upgrade

I have this simple PHP script (authors.php) to rotate through a list of images in a random order

<?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};
?>

It was working fine until I upgraded from PHP 5.6 to PHP 7.2.34.

Now I get this error.

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

What needs to be changed in order for this script to work?

Thanks

Advertisement

Answer

Try this instead of long,long lines

$random = mt_rand(1,9);
echo "<img src="/gallery/authors/{$random}.jpg">";
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement