Search code examples
javascriptphpjqueryhtmlquotes

PHP double and single quotes causing trouble


I have a jQuery script: $('[id="a"][f-id="0"]').val(<?php echo $a; ?>) Now i want to put this script inside a PHP variable $output and return it via the PHP function. But those quotes in script are causing lots of syntax errors. Can anyone make this running?


Solution

  • If I understand correctly, you want to put the Javascript string in your question into a PHP variable. This would look like this:

    $myVar = "$('[id=\"a\"][f-id=\"0\"]').val($a)";
    

    The backslashes () before the double quotes are escape characters that let PHP know that you want the actual character " to be in the string, otherwise PHP thinks you want to end the string.

    Note that the $a doesn't need to be echoed in this context, because you're not outputting $a, but rather including it in a string.