Dragen Posted June 8, 2007 Share Posted June 8, 2007 I'm having some trouble returning a value with a function. It's my first look at using return and It looks fine to me.. it's for an image rotation script. <?php function imagerot(){ $dir = 'testimages/'; if(is_dir($dir)){ if($opdir = opendir($dir)){ while(($images = readdir($opdir)) !== false){ if(filetype($dir . $images) == 'file'){ $imagea[] = $dir . $images; } } closedir($opdir); $imagen = rand(0, count($imagea)-1); $size = getimagesize($imagea[$imagen]); $image = $imagea[$imagen] . '" ' . $size; }else{ $image = $dir . 'default.gif'; } }else{ $image = $dir . 'default.gif'; } return $image; } ?> <img src="<?php imagerot() ?>" /> The problem is when I use return $image; it doesn't seem to return anything so I get an image where the filename is the name of the page. If I use echo $image however it works fine, but I'm really wanting to get return to work. Any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted June 8, 2007 Share Posted June 8, 2007 you need to echo the value out... either swap return $image for echo $image OR repalce this <img src="<?php imagerot() ?>" /> with $im = imagerot() ; ?> <img src="<?php echo $im; ?>" /> Quote Link to comment Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 so there's no way of returning a value? I was just reading up on it and seemed like I could do that.. or is that only for use with certain things? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted June 8, 2007 Share Posted June 8, 2007 it is returning a value now - you just aren't doing anything with that value. Notice in my second solution I assign the returned value to a var called $im and then echo that out... Quote Link to comment Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 oh, okay. I get it now Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.