PHP runs on the web server, creating an html page (or whatever) that the browser will display. JavaScript is executed on the client side, in the user's browser, so php has, by definition, a ready-made executable file before executing JavaScript.
, , , php , JavaScript . ; , , , JavaScript $username , PHP / ; :
<script type="text/javascript">
alert("<?php echo $username; ?>");
</script>
<?php $username = "abc"; ?>
, :
<script type="text/javascript">
alert("");
</script>
, script, , - , , . :
<?php $username = "abc"; ?>
<script type="text/javascript">
alert("<?php echo $username; ?>");
</script>
php / , set/available alert().