PHP SOLUTIONS: DYNAMIC WEB DESIGN (Web site domain) MADE EASY number

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY number passed to rand(). It s much easier to get PHP to count them for you, and that s exactly what the count() function does: it counts the number of elements in an array. You need a number one less than the number of elements in the array, so the second argument passed to rand() becomes count($images)-1, and the result is stored in $i. If you re new to PHP, you may find it hard to understand expressions like $i = rand(0, count($images)-1). All that s happening is that you re passing an expression to rand(), rather than the actual number. If it makes it easier for you to follow the logic of the code, rewrite it like this: $numImages = count($images); // $numImages is 8 $max = $numImages 1; // $max is 7 $i = rand(0, $max); // $i = rand(0, 7) The random number is used in the final line to build the correct pathname for the selected file. The variable $images[$i] is embedded in a double-quoted string with no whitespace separating it from surrounding characters, so it s enclosed in curly braces. Arrays start at 0, so if the random number is 1, $selectedImage is images/maiko.jpg. 2. Open index.php and include random_image.php by inserting the command in the same code block as title.inc.php like this: Since random_image.php doesn t send any direct output to the browser, it s quite safe to put it above the DOCTYPE without forcing browsers into quirks mode. 3. Scroll down inside index.php and locate the code that displays the image in the maincontent

. It looks like this:
Water basin at Ryoanji temple

4. Instead of using images/basin.jpg as a fixed image, replace it with $selectedImage. All the images have different dimensions, so delete the width and height attributes, and use a generic alt attribute. The code in step 3 should now look like this:

ftp web hosting services.

Leave a Reply