Need some help with simple function

Discussion in 'PHP' started by absentx, Aug 24, 2011.

  1. #1
    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.
     
    absentx, Aug 24, 2011 IP
  2. iBank ™

    iBank ™ Peon

    Messages:
    63
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    0
    #2
    <?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 ?
     
    iBank ™, Aug 24, 2011 IP
  3. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yeah I forgot to include $result_random_auto = rand(1, 100); in my original post...but it has always been in my code.
     
    absentx, Aug 24, 2011 IP
  4. insert

    insert Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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)] ;)
     
    insert, Aug 24, 2011 IP
  5. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #5
    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:
     
    ssmm987, Aug 25, 2011 IP
  6. kanikakaminial

    kanikakaminial Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    there is random library is not found
     
    kanikakaminial, Aug 25, 2011 IP
  7. rachellouise

    rachellouise Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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:
     
    rachellouise, Aug 25, 2011 IP
  8. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    absentx, Aug 25, 2011 IP
  9. insert

    insert Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yeah, thanks... I just wanted to show you the easiest way ;)
     
    insert, Aug 25, 2011 IP