Using JavaScript in Your PHP Code
This is a very simple way to include JavaScript within PHP. I am not recommending you go crazy with this function but there are times it can come in handy. Be sure to pay close attention to line four (4) and adjust accordingly. You can also experiment with the return error value if desired.
function js_link($src)
{
//set root path to scripts folder
if(file_exists("my/html/root/" . $src))
{
//we know it exists within the above path
return sprintf("<script type="text/javascript" src="%s"></script>",$src);
}
return "<!-- Unable to load " . $src . "-->";
}
To call the function in your PHP code change the path to your JavaScript folder/file:
echo js_link("folder/to/yourscript.js");
Disclaimer: Before putting any type of code or advice into production from this site, you should consult a professional. The code and advice here is for educational purposes only. It is your responsibility to ensure code safety.
Rate for Encouragement
[Total: 1 Average: 5]