0

I'm really new at php just doing some work, I want to save images in a php array and then show them in the screen, but I cannot save them or display them.

<?php 
$min = 1;
$max = 9;
$number1 = rand($min,$max);

for ($i=1 ; $i<=$number1 ; $i++){

$firstN [$i] = echo "<img src='index.jpg' border='0'>";
}   


echo $firstN [1];
?>

This is what I got , and the last line is to test it but nothing works, I google the topic but it doesn't help. Thanks in advance.

1
  • 3
    Your syntax is wrong - $firstN [$i] should be $firstN[$i] (without the space) Commented Oct 6, 2013 at 23:19

1 Answer 1

3

As long as index.jpg is in the same directory as your file, this should work:

<?php 
$firstN = array();
$min = 1;
$max = 9;
$number1 = rand($min, $max);
for ($i = 0; $i < $number1; $i++){
    $firstN[] = '<img src="index.jpg" border="0">';
}   

echo $firstN[0];
?>

Cleaned up the code a bit. When storing information in the array, you don't use echo and, like Mister pointed out, you had a space in the echo at the bottom of the code between the array-variable and the brackets.

Sign up to request clarification or add additional context in comments.

4 Comments

@MisterMelancholy - Sorry, you beat me to it. Added information now.
Yeah, I saw that and removed my comment. One last thing I noticed though - Your array start at 0, and the original array started at 1. You should probably state that somewhere and point out that $min should be changed to 0 (else image 0 will never be displayed)
@MisterMelancholy - I just changed how the array stores the images. It started at 1, but it normally starts at 0. The variable $number1 does not say anything about if the image with index 0 should be displayed, but how many images should be added to the loop. As it is right now it will add anything from 1 to 9. I changed the forloop to start at 0 and doing a lesser than rather than lesser or equal. The code is the same, just adding the images at index 0 instead of 1, the loop itself will be executed just like in OP's code.
Thanks for your help I realized what was wrong with this, I'm having some troubles with the syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.