Struggling to understand why the foreach loop in the function doesn’t print out all of the values in the includes array. Any help is much appreciated.
Array $this->includes:
Array (
[0] => http://localhost/assets/css/style.css
[1] => http://localhost/assets/css/internal.css
[2] => http://localhost/assets/css/custom-theme/jquery-ui-1.8.17.custom.css
[3] => http://localhost/assets/js/jquery-ui-1.8.16.custom.min.js
)
Function:
public function print_includes()
{
print_r($this->includes);
// Initialize a string that will hold all includes
$final_includes = '';
foreach ($this->includes as $include)
{
// Check if it's a JS or a CSS file
if (preg_match('/js$/', $include))
{
// It's a JS file
$final_includes .= '<script type="text/javascript" src="' . $include . '"></script>';
}
elseif (preg_match('/css$/', $include))
{
// It's a CSS file
$final_includes .= '<link href="' . $include . '" rel="stylesheet" type="text/css" />';
}
return $final_includes;
}
}
What’s printed with $this->layouts->print_includes():
<link href="http://localhost/assets/css/style.css" rel="stylesheet" type="text/css" /></head>
Your return statement is inside the foreach loop, so it only ever concatenates the first link element and then returns the string as the result of the function. Try moving it outside the loop.