var username = "<?php echo $username ?>";
In the above code, we simply defined a new variable in JS and named it as username. We then assigned the PHP variable $username to that JS variable by echoing it. Naturally, the $username variable should be defined previously in your page, above this line.
Note that we used quotes (") around the PHP code that outputs the $username, since using quotes around strings is required when declaring a variable in JS. If the PHP output is a number, you don't need to use quotes. But make sure to use the quotes correctly in case the output string may also contain quotes inside it, hence break the JS code.
In the second method, the JS code is stored in an external JS file (e.g. script.js) and the file is linked from the HTML page. For example, if you want to pass a variable time value (in seconds) to a JS timer, you can use the following code:
var seconds = <?php echo $seconds ?>;
And something like this in your external JS file (script.js):
window.location.href = "/";
}, seconds * 1000);
More PHP Tips
How to Get All Files and Folders in a Folder with PHP How to Download a File with PHP How to Control CSS Variables with PHP How to Create a ZIP Archive with PHP How to Get the Subfolders in a Folder with PHP How to Create a Cookie with PHP How to Delete a Cookie with PHP How to Get the Number of Files in a Folder with PHP How to Get All Files in a Folder with PHP PHP Image Watermark: How to Add a Watermark to Images in PHPPHP Tips