Page 1 of 1

help with functions

Posted: Thu Jan 23, 2003 2:59 am
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?

Posted: Thu Jan 23, 2003 3:50 am
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

Posted: Thu Jan 23, 2003 12:00 pm
by zerodegreeburn
w00t , thanks so much :)

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