Learning PHP from a book - code error

I am trying to learn PHP from a book and I have got stuck and I dont know enough yet to figure it out.
I get an error Fatal error: Cannot use [] for reading in C:\wamp\www\includes\random_image.php on line 4

from this code

[php]<?php
$images = array(‘kinkakuji’, ‘maiko’, ‘maiko_phone’, ‘monk’, ‘fountains’, ‘ryoanji’, ‘menu’, ‘basin’);
$i = rand(0, count($images)-1);
$selectedImage = “images/{$images[$i]}.jpg”;[/php]

Any ideas?

Try just echoing the variable $i after you do the rand() function. You could also try changing the $selectedImage= line to the following:

[php]$selectedImage = “images/”.$images[$i].".jpg";[/php]

Unfortunately that code hasnt worked either :frowning:

Could the problem be that the book is written around php 5.3 and not 5.4?

No, did you try echoing or printing $i before the $selectedImage line to make sure that it was being set correctly?

In fact change the code to this:
[php]$images = array(‘kinkakuji’, ‘maiko’, ‘maiko_phone’, ‘monk’, ‘fountains’, ‘ryoanji’, ‘menu’, ‘basin’);
$i = rand(0, count($images)-1);
var_dump($i);
$selectedImage = “images/”.$images[$i].".jpg";[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service