Hiya!

I'm new here 😃

I'm struggling to find out if it's possible to call a function from within a variable. I'll show you what I mean:

<?php

function MyAds($arg)
{
$openads = file("$arg/ads.txt");
$selectedad = $openads[rand(0, count($openads) - 1)];
$selectedad = trim($selectedad);
echo "$selectedad";
}

$adcategory = "Games";

$gamesad1 = "Here is the ad MyAds($adcategory)";

echo $gamesad1;

?>

I know this doesn't work, but is there another way around it? Is there some way I can call a function with an echo? eg.

echo "Here is the ad MyAds($adcategory)";

Any help would be greatly appreciated!

Matt

    I think this should do

    <?php 
    function MyAds($arg) 
    { 
      $openads = file("$arg/ads.txt"); 
      $selectedad = $openads[rand(0, count($openads) - 1)]; 
      $selectedad = trim($selectedad); 
      // return rather than echo
      return $selectedad; 
    } 
    
    $adcategory = "Games"; 
    
    //concatenate (join) the two strings together
    $gamesad1 = 'Here is the ad ' . MyAds($adcategory); 
    
    echo $gamesad1; 
    ?> 
    

      thanks sarah that worked perfectly 🙂

        I have another question! 😃

        Inside ads.txt I have the following lines:

        <a href="http://www.sponsor.com?1231"><img src="http://adserver.com/Games/1.gif" alt="$sponsoralt" border="0"></a>

        <a href="http://www.sponsor.com?1231"><img src="http://adserver.com/Games/2.gif" alt="$sponsoralt" border="0"></a>

        Using this function:

        <?php 
        function MyAds($arg) 
        { 
          $openads = file("$arg/ads.txt"); 
          $selectedad = $openads[rand(0, count($openads) - 1)]; 
          $selectedad = trim($selectedad); 
          // return rather than echo
          return $selectedad; 
        } 
        
        $adcategory = "Games"; 
        
        //concatenate (join) the two strings together
        $gamesad1 = 'Here is the ad ' . MyAds($adcategory); 
        
        echo $gamesad1; 
        ?> 
        

        Is there a way to make the $sponsoralt in ads.txt a variable? At the moment it's just displaying "$sponsoralt" which is really not that useful 🙂

        Thanks!

        Matt

          <a href="http://www.sponsor.com?1231"><img src="http://adserver.com/Games/2.gif" alt="<?php echo $sponsoralt; ?>" border="0"></a>
          

          you can stop and start php mode without losing the variables you have already created.

            Thanks for the quick reply!

            I probably should have been more specific, because I'm actually using this script to generate static pages, so having <?php tags doesn't work.

            Any other ideas?

              I'm actually using this script to generate static pages

              huh?

                Write a Reply...