# PHP random backrground

**URL:** <https://forum.kirupa.com/t/php-random-backrground/18760>\
**Category:** programming\
**Created:** [April 20, 2003, 6:40pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760 "2003-04-20T18:40:12Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlk](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@mlk](https://forum.kirupa.com/u/mlk)\
**Post date:** [April 20, 2003, 6:40pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/1 "2003-04-20T18:40:12Z")

</div>

could someone help me with a bit of php code ???

im trying to generate 4 different backgrnds in php  
in my directorie are 4 jpg named 1,2,3 and 4  
why doesnt this work:

```php

<html>
<body background="<?php echo rand(1,4) ?>.jpg">
</body>
</html>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 20, 2003, 7:06pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/2 "2003-04-20T19:06:33Z")

</div>

what erroe message does it give ya? try

```php
<?php echo(rand(1,4)); ?>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 20, 2003, 7:11pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/3 "2003-04-20T19:11:05Z")

</div>

oh… and i would rather use a client-side languae for simple thing like these 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 20, 2003, 7:19pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/4 "2003-04-20T19:19:27Z")

</div>

problem is i dont think java would be capable of editing the background image - maybe I should check…  
Your code does the same thing mine did:  
[http://mlkdesign.free.fr/php/tryout2.php3](http://mlkdesign.free.fr/php/tryout2.php3)

It only displays the file named 1.jpg…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 20, 2003, 7:26pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/5 "2003-04-20T19:26:57Z")

</div>

this file has the same script i posted above, it does print a random number between 1 and 4… besides javascript IS capable of doing this, search the forums 🙂

[http://24.141.60.208/ran.php](http://24.141.60.208/ran.php)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 20, 2003, 7:49pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/6 "2003-04-20T19:49:30Z")

</div>

I have a javascript code that puts a random image in a page, i don;t know how you would do it for a background, but if u want, i could give u the code.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 20, 2003, 9:22pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/7 "2003-04-20T21:22:35Z")

</div>

i know the code for the image, and the php script does work for a normal image, it’s just that it wont work with the background, i have no clue why.  
Ill try to post this on a specialised php phorum…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 20, 2003, 9:27pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/8 "2003-04-20T21:27:14Z")

</div>

oh… sorry, i didnt read the thread to well  
good luck to ya

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 20, 2003, 10:46pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/9 "2003-04-20T22:46:18Z")

</div>

you have to enable the random function for php… hold on let me find it…

you have to use srand() to initialize the random-ness in php.

[http://www.php.net/manual/en/function.srand.php](http://www.php.net/manual/en/function.srand.php)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 20, 2003, 10:55pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/10 "2003-04-20T22:55:38Z")

</div>

hmm… i used the exact script i posted about and it does generate a random number… i didnt ‘initialize’ it 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 20, 2003, 10:58pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/11 "2003-04-20T22:58:53Z")

</div>

its only the older versions. His server might be behind… just a probable suggestion. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 20, 2003, 11:10pm UTC](https://forum.kirupa.com/t/php-random-backrground/18760/12 "2003-04-20T23:10:57Z")

</div>

my server allows php4 and all that. i dont think it is way behind… What I might do is try to generate a random CSS background in php and see if it works…

What I need now is some sleep.
