FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

showing off the code I just made and ask for suggestions

 


Mgccl
showing off the code I just made and ask for suggestions
This is the code that randomly select an picture file(you define) and show it!... can be used on forums!
any one have any feature requests? because I think I got nothing need to upgrade
Code:
<?php
$url[] = "a.jpg";
$url[] = "b.gif";
$url[] = "c.png";
$rand = array_rand ($url);
$file = $url[$rand];
$path = pathinfo ($file);
switch ($path['extension']) {
case "jpg";
header ( "Content-Type: image/jpeg" );
break;

case "gif";
header ( "Content-Type: image/gif" );
break;

case "png";
header ( "Content-Type: image/png" );
break;
}
readfile ($file);
?>
n0obie4life
You sure it works?

Doesn't seem correct.
rohan2kool
firstly, the arrays are not declared properly. the function array_rand will always return c.png. Instead declare the array as:

Code:

$url = array("a.jpg", "b.gif", "c.png");

//or you can also declare as:

$url = array();
$url[0] = "a.jpg";
$url[1] = "b.gif";
$url[2] = "c.png";


Rest everything is alright.
Mgccl
in PHP5 it don't need $url = array(); because my book said nothing about that...
I think it is needed in lower version of PHP, but looks like add that line won't hurt
rohan2kool
you don't need that in any php versions. It's just a good practice. It tells the php parse which kind of variable it has to handle hence thereby giving you an advantage on the memory side.

[edit]

I am extremely sorry.. but your code is 100% correct. When you use $myarray[] = somevalue , the somevalue is actually appended to the array.. so ur code is completely correct. Sorry for that.

rohan
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.