Avatar ·

How to pass variables and data from PHP to JavaScript?

Hello everyone! I'm a beginner PHP developer and I've run into a problem. I'm writing a simple PHP website and I need to pass some data from the server side (PHP) to the client side (JavaScript). For example, I have a variable `$username = 'Ivan';` and I want to display a greeting on the page via JS: `alert("Hello, " + username);`.\n\nThe problem is that PHP runs on the server and JS runs in the browser. I tried to simply insert PHP code inside a JS script, like this:\n\n```php\n\n```\n\nBut I get an error in the browser console: `Uncaught SyntaxError: Unexpected identifier`. If the variable contains a number, it works, but with strings it doesn't. I understand that PHP outputs the string without quotes, and JS interprets it as a variable name.