help with functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

help with functions

Post by zerodegreeburn »

ok, i have this code:

Code: Select all

<?php

function myfunction($num) {
    for($i = 1; $i < $num; $i++) {
	  include ('config.php');
	  echo "$site0.$i._name";
    }
  }

  myfunction(3);

?>
what i want to thing to echo is the variables in the config.php file

they are $site01_name, $site02_name, etc etc

i think ive just got the syntax messed up?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try:

Code: Select all

<?php
function myfunction($num) { 
    include 'config.php'; // you only need to include the file once so
                                 // don't put it in the for loop.
    for($i = 1; $i <= $num; $i++) { 
        echo ${'site0'.$i.'_name'}; // construct the variable name and echo()
    } 
} 
?>
For more info:
http://www.php.net/manual/en/language.v ... riable.php

Mac
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

Post by zerodegreeburn »

w00t , thanks so much :)

quick and informative response as-per-usual, i love this place
Post Reply