logo-bns-app.png



« What is Web Design? | Index | About AtomZ »

February 09, 2004, by Léon Krijnen

Another way leading to Rome   

rom.gif
Another way to randomize things, be it pictures, cookies, headlines, entries, postings, archives, whatever.
This one in php, and maybe just a bit simpler, more elegant?

I think so.

My previous method consisted of a server side include call code somewhere (in a template, or in static file, parsed from the template) calling an executable file in the cgi-local, that on its turn executed the randomizer. Whic means you need the call, the script and a file with what you want to randomize.
With the php method you only need a little bit of code, and a file with the randomizer stuff.
Remove the hard breaks, and you have one line of code. Silly? I know a guy who is really freakish about file and code sizes. After having done his html he removes all hard returns form his code, and all the file-names on his server a as short as possible. He runs files like a.htm trough z.htm, and is convinced it speeds up things.

Anyway, the php randomizer, to be embedded anywhere you want it to show up, looks like this:

<?php
$randomizer = file("random.dat");
$random = rand(0, sizeof($randomizer)-1);
echo $randomizer[$random];
?>

In this case the name of the file containing the randomizer is, you guess it: random.dat.

Bring it back to one line of code, and it still works, at least it does on my server;

<?php $randomizer = file("random.dat"); $random = rand(0, sizeof($randomizer)-1); echo $randomizer[$random]; ?>

And of course, my freakish code saving friend would do this:

<?php $a = file("f"); $b= rand(0, sizeof($a)-1); echo $a[$b]; ?>

Works too, funny, isn't it?

Posted: February 9, 2004 06:49 PM (261 words).   

Comment over here or on my Facebook wall . . .