什么是在PHP中回显脚本的正确方法?

I want to use this script on a conditional statement, hence why i thought of echoing it out in the first place.

echo "<script type='text/javascript'>

window.onload = function() {
    if(!window.location.hash) {
        window.location = window.location + '#loaded';
        window.location.reload();
    }
}
</script>";

The correct way is to not echo out html with PHP. It's a really bad practice. You should end your PHP before printing the HTML.

echo "<script type='text/javascript'>
".
    "    window.onload = function() {
".
    "        if(!window.location.hash) {
".
    "            window.location = window.location + '#loaded';
".
    "            window.location.reload();
".
    "        }
".
    "    }
".
    "</script>";