This is the Message Centre for R'win

A1021645 - PHP/Random bgcolor

Post 1

OwlofDoom

A1021645

Hi there R'Win...

I've been assigned to sub-edit your entry on PHP/bgcolor. It makes an interesting read, but I can't help but think this is all a little PHP3. Have you heard of the array_rand() function?

Here's how I'd do it...

<?php
$colours = array('#ffffff','#bbbbbb','#800080');
$bgcolour = $colours[array_rand($colours)];

echo "\n";
?>

smiley - biggrin ...

That's for your benefit, mind. I'll wait for your approval to update the entry. smiley - tongueout

~ smiley - towel


A1021645 - PHP/Random bgcolor

Post 2

R'win

I have heard of that function. The original random script is the first array-less one. The array was an idea I had while writing the entry and at the time I hadn't really used arrays that much; now I've discovered how versatile and useful they can be.
Now, that is probably they way I would do it, too. Feel free to change it.


A1021645 - PHP/Random bgcolor

Post 3

OwlofDoom

I think I'd rather add it, and change the way you've defined your arrays... there's no need to do a count in that C-style, as PHP has a (iirc) count($array) function to do it for you.

I can be your PHP guru if you like. smiley - winkeye Take a look at http://owlsound.co.uk/me/ which was all written in that wonderfully system-hogging language. smiley - biggrin

~ smiley - towel


A1021645 - PHP/Random bgcolor

Post 4

R'win

> I think I'd rather add it, and change the way you've defined your arrays

That's what I said (or at least that's what I meant). I agree entirely; now that I know more about arrays I see that that is a much better way of doing things and would do it like that myself has I done the entry now instead of back then.

> PHP has a (iirc) count($array) function

yes, it does; I've used it

> I can be your PHP guru if you like

No thanks; I believe I'm enough of a 'guru' (in knowldge if not age and beard length smiley - smiley) to manage on my own

> which was all written in that wonderfully system-hogging language.

If you mean PHP, then http://richardwinskill.co.uk is similarly powered (I can assure you there is no javascript anywhere) and http://galsing.richardwinskill.co.uk is the most powerful and versitile PHP script I've ever written, if i do say so myself... smiley - smiley


A1021645 - PHP/Random bgcolor

Post 5

OwlofDoom

Excellent stuff, sir! smiley - ok

I didn't mean any offence by offering my services. smiley - biggrin I've moved onto better climes since my PHP days (I'm a python man now, but I suspect that'll change in time).

I was a little surprised that you knew the ${$string}} trick for introspective variable reference and didn't know about arrays, mind. I couldn't live without my arrays (well, more specifically, lists and hashes/dictionaries - which is what PHP arrays really are)!

http://owlsound.co.uk/me/mm.php is my crowning achievement in PHP but I won't dare show you the source code as it's a complete nightmare. I suppose I should get round to writing a nice clean OO Python version of it sometime (note what I'm the Keeper of ... smiley - winkeye).

~ smiley - towel


A1021645 - PHP/Random bgcolor

Post 6

OwlofDoom

Excellent stuff, sir! smiley - ok

I didn't mean any offence by offering my services. smiley - biggrin I've moved onto better climes since my PHP days (I'm a python man now, but I suspect that'll change in time).

I was a little surprised that you knew the ${$string}} trick for introspective variable reference and didn't know about arrays, mind. I couldn't live without my arrays (well, more specifically, lists and hashes/dictionaries - which is what PHP arrays really are)!

http://owlsound.co.uk/me/mm.php is my crowning achievement in PHP but I won't dare show you the source code as it's a complete nightmare. I suppose I should get round to writing a nice clean OO Python version of it sometime (note what I'm the Keeper of ... smiley - winkeye).

~ smiley - towel


A1021645 - PHP/Random bgcolor

Post 7

R'win

I did know a bit about arrays, I just hadn't come across the array_rand() function at the time

I've done a bit of Python, but nothing really advanced; the most I've done is a small text-based adventure game and a simple Asteroids game

I like the mastermind script


A1021645 - PHP/Random bgcolor

Post 8

OwlofDoom

array_rand() is actually a really simple PHP function to write:

function array_rand($array) {
return rand(count($array));
}

... mind you, that only works for numerical arrays, like the one in the example... the real array_rand() can return keys for hashed arrays too...

~ smiley - towel


A1021645 - PHP/Random bgcolor

Post 9

R'win

I know about array_rand() *now*; I've used it a fair bit. I just hadn't met it *then*


A1021645 - PHP/Random bgcolor

Post 10

OwlofDoom

Blimey! Is this entry old, or are you just picking up PHP fast? I learned about array_rand() late too as my book (PHP4 Bible) does appear to leave it out altogether... smiley - biggrin

~ smiley - towel


A1021645 - PHP/Random bgcolor

Post 11

R'win

Well, both really. The entry was created on Dec 5th 2002 so it's a good few months old, and I pick up the odd function here and there; mainly from the PHP documentation at http://www.php.net.


A1021645 - PHP/Random bgcolor

Post 12

OwlofDoom

Do you mind if I change your awfully complicated while() loop for something more useful in your article?

I think the following:

$colours = array(
"#ffffff",
"#ffff99",
"#00ccff",
"#66ff99",
"#ffcc00",
"#d7ebff",
"#ccffcc"
);

is better than your:

$c1 = "#ffffff";
$c2 = "#ffff99";
$c3 = "#00ccff";
$c4 = "#66ff99";
$c5 = "#ffcc00";
$c6 = "#d7ebff";
$c7 = "#ccffcc";

$colours = array();

$n=1;

while(isset(${"c$n"})){
$colours[] = ${"c$n"};
$n++;
}

but I won't change it if you'd not like me to.

~ smiley - towel

PS I'm sorry about being so late here, I got tied up with project work and I've only just been freed from the cage!


A1021645 - PHP/Random bgcolor

Post 13

R'win

Feel free.
Now that I've had more experience of PHP I know that's better and I would do it that way myself were I to re-do the entry


A1021645 - PHP/Random bgcolor

Post 14

OwlofDoom

Thanks... Sorry to have kept you waiting. Expect a sub-edited entry by the end of the week. smiley - smiley


A1021645 - PHP/Random bgcolor

Post 15

R'win

No hurry


A1021645 - PHP/Random bgcolor

Post 16

OwlofDoom

Well, I don't want the entry to celebrate its anniversary before going into the EG!


A1021645 - PHP/Random bgcolor

Post 17

R'win

I doubt you work *that* slowly...


A1021645 - PHP/Random bgcolor

Post 18

OwlofDoom

...you want to bet on that? smiley - winkeye


A1021645 - PHP/Random bgcolor

Post 19

R'win

I don't know. Do you?


A1021645 - PHP/Random bgcolor

Post 20

R'win

There should be a " work that slowly" after "Do you"...


Key: Complain about this post

More Conversations for R'win

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more