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 Link to comment https://forums.phpfreaks.com/topic/54680-solved-return-not-returning/ 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; ?>" /> Link to comment https://forums.phpfreaks.com/topic/54680-solved-return-not-returning/#findComment-270414 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? Link to comment https://forums.phpfreaks.com/topic/54680-solved-return-not-returning/#findComment-270418 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... Link to comment https://forums.phpfreaks.com/topic/54680-solved-return-not-returning/#findComment-270425 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 Link to comment https://forums.phpfreaks.com/topic/54680-solved-return-not-returning/#findComment-270428 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.