[Solved] PHP function with looping variabel [closed]


You can define functions in a loop like this:

for ($i=1; $i<=2; $i++) {

$code = <<<EOD
function writeMsg{$i}() {
  echo 'Hello World!';
}
EOD;

eval($code);
}

writeMsg1();

It outputs:

Hello World!

This code uses a heredoc syntax (<<<EOD EOD;) to define the function and the eval() function which evaluates the code.

0

solved PHP function with looping variabel [closed]