The way you are looping through the array, using :
foreach($array as $var => $content) {
// ...
}
makes me think you should use an associative array for your $array
.
i.e., it should be declared this way :
$array = array("HEADER" => "Welcome to ".NAME."'s website!");
And, as a demonstration, here's an example of code :
$template = <<<TPL
Hello, World !
{HEADER}
And here's the content ;-)
TPL;
define("NAME","Site Name");
$array = array("HEADER" => "Welcome to ".NAME."'s website!");
foreach($array as $var => $content) {
$template = str_replace("{" . strtoupper($var). "}", $content,$template);
}
var_dump($template);
And the output I get it :
string 'Hello, World !
Welcome to Site Name's website!
And here's the content ;-)' (length=73)
Which indicates it's working ;-)
(It wasn't, when $array
was declared the way you set it)
When using this :
$array = array("HEADER","This is the header");
var_dump($array);
You are declaring an array that contains two items :
array
0 => string 'HEADER' (length=6)
1 => string 'This is the header' (length=18)
On the other hand, when using this :
$array = array("HEADER" => "This is the header");
var_dump($array);
You are declaring an array that :
- contains only one item
- "HEADER" being the key
- and "This is the header" being the value
Which gives :
array
'HEADER' => string 'This is the header' (length=18)
When using foreach ($array as $key => $value)
, you need the second one ;-)
And, purely for reference, you can take a look at, in the manual, the following pages :