Okay I have been skating around using my own functions in php for a year or two now and I figure it is probably time to grow up and start using functions...but I must still have a fundamental misunderstanding of how they work, because I cannot get this function - which I believe to be quite simple - to work. I assume I am just attacking it in the wrong way: <?php function ad_randomizer() { if($result_random_auto<=50){ $ad= 'advertisement option one'; } elseif($result_random_auto>=51){ $ad= 'advertisement option two'; } } // a bunch of code ad_randomizer(); //------------------------------------------- So when I call ad_randomizer nothing is happening. I understand that I need my function to "return" a value, but I have tried various incarnations of that and nothing worked either. Any help appreciated.
<?php $result_random_auto = rand(1, 100); $ad = "default advertisement option"; function ad_randomizer() { if($result_random_auto <= 50) { $ad = 'advertisement option one'; } elseif($result_random_auto >= 51) { $ad = 'advertisement option two'; } } /* Other code */ ad_randomizer(); echo $ad; /* Other code */ ?> PHP: Is this what you meant ?
yeah I forgot to include $result_random_auto = rand(1, 100); in my original post...but it has always been in my code.
Yeah, you should either return value or print it directly. For example: <?php function ad_randomizer() { if($result_random_auto<=50){ $ad= 'advertisement option one'; } elseif($result_random_auto>=51) { $ad= 'advertisement option two'; } return $ad; } $var = ad_randomizer(); echo $var; ------------------ or ------------------ function ad_randomizer() { if($result_random_auto<=50){ echo 'advertisement option one'; } elseif($result_random_auto>=51) { echo 'advertisement option two'; } return $ad; } ad_randomizer(); -------------------------------- But the best way to do this will be to store the ads in array, and then simply call $adsArray[array_rand($adsArray)]
Well, the $result_auto_randomizer is defined outside the function. You can either copy and paste that line inside the function, global the variable, or pass the variable when the function is called: e.g. $result_random_auto = rand(1, 100); $ad = "default advertisement option"; function ad_randomizer($result_random_auto) { if($result_random_auto <= 50) { $ad = 'advertisement option one'; } elseif($result_random_auto >= 51) { $ad = 'advertisement option two'; } } echo ad_randomizer($result_random_auto); PHP:
function ad_randomizer(){ $result_random_auto=rand(1,100); if($result_random_auto<=50){ $ad= 'advertisement option one'; } elseif($result_random_auto>=51){ $ad= 'advertisement option two'; } print $ad;} // a bunch of code ad_randomizer(); PHP:
Excellent, really appreciate everyones help. I now have a working function and have written a few more as well Insert...I like the way to do it with the array...Thats the thing about learning PHP...there are 500 different ways to do everything, so I always like to get the opinions of those with much more experience. Thanks again.