View Full Version : Css and PHP

05-24-2011, 08:39 PM

I am trying to have a random background image load each time I have my page load. To do so, I tried the following:

$backs[0] = "background1.gif";
$backs[1] = "background2.gif";
$backs[2] = "background3.gif";
$randBackNum = rand(0,2);

body { background: url($backs[$randBackNum]) 50% 50% no-repeat fixed; }

Except that it seems I can't call the php variable from the css statement because I'm outside the PHP tags. However, if I try to stick the css statement into the PHP tags I naturally get an error as it's trying to interpret that statement as php code....

As you may have assumed I do not know much about this subject matter; css and php are new to me, I only know basic html, so I would appreciate any help that can be provided. I hope that what I am asking for is simple enough. Thanks!


P.S. The css formatting is correct for what I want to do when I hard code in an image file.

05-24-2011, 08:43 PM
body { background: url(<?php print $backs[$randBackNum]; ?>) 50% 50% no-repeat fixed; }