w3b-Doctor - Clean Simple Web Design

Welcome to w3b-Doctor.com The home of clean simple web design.

Using built in function with php

You will probably be using built in functions without even noticing you are using them when you start your “learning to program journey”. Probably the most simple one is echo, here is an example:


<?php

$yourName = “Bob”;

// will output “hello bob”

echo “hello ” . $yourName;

?>

But lets take it a bit further for this blog, to show it in a simple script. Say you want to randomise the background image on a webpage. Start of by setting up your images with a progressive name and store them in the same folder:
myImage-1.jpg
myImage-2.jpg
myImage-3.jpg
myImage-4.jpg
myImage-5.jpg

Then you can use the following simple script to dynamically set which image will appear.

The php:


<?php

$myRandNum = rand(1, 5);

?>

The css:


<style>

#header{background-image: url('images/myImage-<?php echo $myRandNum ?>.jpg') no-repeat;}

</style>

rand() is a built in function in php and selects a random number between the two stated parameters, in this case it would be 1 and 5. For more info on this function you can check out the php manual http://www.php.net/manual/en/function.rand.php

© Copyright 2013

w3b-Doctor Blog Terms And Conditions Of Use

All content provided on the w3b-Doctor.com blog is for informational purposes only. We makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site.

The owner of w3b-Doctor.com will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.

This terms and conditions is subject to change at anytime with or without notice.