In the view, right before the footer, I want to load three JavaScript files. So, the controller sets three variables. For example, $js_1 = "bootstrap.js", $js_2 = '' and $js_3 = "tinymce.js", which may or may not be empty. The logic is, it should echo only if the variable is not empty. I also want to use the ternary if operator.
This was the best I could try.
for ($i = 1; $i <= 3; $i++)
{
echo (!empty ('$js_' . $i)) ? get_jscript('$js_' . $i) : NULL;
}
The function get_jscript() simply returns the HTML script src.
function get_jscript($js)
{
return '<script type="text/javascript" src="' . $js . '"></script> ';
}
Please note that I intend to ask this question to primarily learn the correct PHP syntax and not just load JS in the view.