Expanding on someone else’s answer:
<script>
var myvar = <?php echo json_encode($myVarValue); ?>;
</script>
Using json_encode() requires:
- PHP 5.2.0 or greater
$myVarValue
encoded as UTF-8 (or US-ASCII, of course)
Since UTF-8 supports full Unicode, it should be safe to convert on the fly.
Note that because json_encode
escapes forward slashes, even a string that contains </script>
will be escaped safely for printing with a script block.
Related Contents:
- Pass a PHP variable to a JavaScript variable
- Convert php array to Javascript
- Highlight row when the checkbox is true
- How to access PHP variables in JavaScript or jQuery rather than [duplicate]
- Is there a PHP function that can escape regex patterns before they are applied?
- How to create websockets server in PHP
- How to escape strings in SQL Server using PHP?
- Send array with Ajax to PHP script
- How to avoid echoing character 65279 in php?
- How can I get the user’s local time instead of the server’s time?
- Is mysql_real_escape_string() broken?
- Send JSON data from Javascript to PHP?
- Access PHP var from external javascript file
- How do I run PHP code when a user clicks on a link?
- Escaping single quote in PHP when inserting into MySQL [duplicate]
- How to pass data from Javascript to PHP and vice versa? [duplicate]
- Variable-length lookbehind-assertion alternatives for regular expressions
- Why is PDO better for escaping MySQL queries/querystrings than mysql_real_escape_string?
- Pass a JS variable to a PHP variable
- How to retrieve Request Payload
- Javascript and PHP functions
- Unserialize PHP Array in Javascript
- Detect if Android app has been installed on the device using a mobile web page – PHP and JS
- The holy grail of cleaning input and output in php?
- How to put php inside JavaScript?
- Send PHP date to JavaScript date format
- UTF-8 safe equivalent of ord or charCodeAt() in PHP
- jQuery AJAX form using mail() PHP script sends email, but POST data from HTML form is undefined
- New lines (\r\n) are not working in email body
- PHP quotes inside quotes
- how to write javascript code inside php
- Can you re-populate file inputs after failed form submission with PHP or JavaScript?
- What is the equivalent of JavaScript’s encodeURIcomponent in PHP?
- How can we open a link in private browsing mode
- Will a script continue to run even after closing a page?
- How to detect country / location of visitor? [duplicate]
- Set Session variable using javascript in PHP
- Scaling a chat app – short polling vs. long polling (AJAX, PHP)
- Do I need to escape backslashes in PHP?
- Automatically detect user’s current local time with JavaScript or PHP
- How can I detect the browser with PHP or JavaScript?
- How to pass a Javascript Array via JQuery Post so that all its contents are accessible via the PHP $_POST array?
- How to replace all XHTML/HTML line breaks () with new lines?
- How to pass an array using PHP & Ajax to Javascript?
- How to rotate image and save the image
- Jquery: Running AJAX locally without a webserver
- Javascript function post and call php script
- How to save webpage as a image file using PHP?
- Is there a built-in function or plugin to handle date formatting in JavaScript?
- browscap ini directive not set