Skip to content
PHP Logo

What are the differences between using double and single quotes in PHP when passing variables to JavaScript functions?

When passing variables from PHP to JavaScript functions, using double quotes can cause issues if the variable contains special characters that need to be escaped. Single quotes are safer to use in this case as they do not interpret variables inside them, preventing any potential conflicts. To ensure proper handling of variables, it's recommended to use single quotes when passing PHP variables to JavaScript functions.

php
<?php
// Using single quotes to pass PHP variable to JavaScript function
$name = "John";
echo "<script>";
echo "var name = '" . $name . "';";
echo "myFunction(name);";
echo "</script>";
?>