Fatal/Uncaught Error - foreach formula for collecting images

I have a minor website for publishing photos, located at www.jondanielsson.com. My knowledge in PHP, HMTL and websites is minimal. I manage to update the website with new photos and picture series, but that is basically it.

The website was coded by an old friend of mine back in 2012, so the php version would be 5.4 or older. Seeing as the web-hotel where the site is stored is moving to php 7.x and do not support older versions anymore I have run into some problems.

PHP error message on website:
Fatal error: Uncaught Error: [] operator not supported for strings in /customers/4/c/c/jondanielsson.com/httpd.www/index.php:27 Stack trace: #0 {main} thrown in /customers/4/c/c/jondanielsson.com/httpd.www/index.php on line 27

This could also be line 30 or 31 depending on the root location.

On the lines resulting an error the following code is:

foreach($images as $image){ $imgs[] = “$image”; }

Formula foreach ($series as $serie) { works as intended.

Any guidance to give on how to alter the foreach code line to get it working as intended? I have referenced below sites, without really figuring out how the foreach can be fixed between versions:

https://www.php.net/manual/en/control-structures.foreach.php
https://www.php.net/manual/en/migration70.incompatible.php

Huge thanks for the support and please let me know if any additional information would be needed.

$imgs has had a string assigned to at some point prior to the posted code. The posted code expects it to be an array.

Note: If that’s all the code inside the loop, looping over an array and assigning the values to another array is a pointless exercise. And assuming the forum software didn’t mangle the posted code, it’s another pointless exercise to put double-quotes around just a php variable if there’s nothing else inside the string.

Hi phdr and thank you for the support.

Adding the full code from the following url:

http://jondanielsson.com/gallery/nature-and-scenery

Located at Homepage -> Portfolio -> Nature and scenery

"

JD PHOTOGRAPHY <?php include("../javascript.php"); ?>
<?php include("../header.php"); ?>

<div id="contentWrap">
	<div id="content">
    
    	<div id="ImageWrapper">
        	<div class="imageSlider">   
            
            	<?php
				//path to directory to scan. i have included a wildcard for a subdirectory
				$directory = "nature-and-scenery-pics/";
 
				//get all image files with a .jpg extension.
				$images = glob("" . $directory . "*.jpg");
				//usort($images, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
 
				$imgs = '';
				// create array
				foreach($images as $image){ $imgs[] = "$image"; }
 
				//shuffle array
				//shuffle($imgs);
 
				//select first 20 images in randomized array
				//$imgs = array_slice($imgs, 0, 20);
 
				//display images
				$i = 0;
				foreach ($imgs as $img) {
					if($i == 0){
						echo "<div class='carouselDiv activeImage'><img src='$img' class='carouselImage' /></div>\n";
					}else{
						echo "<div class='carouselDiv'><img src='$img' class='carouselImage' /></div>\n";
					}
					$i++;
				}
				?>
                
            </div>
            
			<div id="prevImage"><div class="arrow-left"></div></div>
			<div id="nextImage"><div class="arrow-right"></div></div>
			<div id="pager"></div>
		</div>
	</div>
</div>
"
			$imgs = '';
			// create array
			foreach($images as $image){ $imgs[] = "$image"; }

so yes, that’s what @phdr already mentioned: $imgs is a string (maybe there was a quirk for this situation in earlier PHP versions) but you want to handle it as an array, and you make a pointless new array.

Sponsor our Newsletter | Privacy Policy | Terms of Service