<?php
$a="cat";
$html=<<<HTML
<html>
<script>
var b ="$a";
document.write(b);
</script>
</html>
HTML;
echo $html;?>
this code displays cat,is this a valid way of equating javascript variables to php variables,
<?php
$a="cat";
$html=<<<HTML
<html>
<script>
var b ="$a";
document.write(b);
</script>
</html>
HTML;
echo $html;?>
this code displays cat,is this a valid way of equating javascript variables to php variables,
Valid is maybe not the word I would use. Does it work, is a more important question to me. In general, because PHP runs at the server before returning the page to the browser, which then interprets and executes JavaScript, you can definitely write in variables that JS will use. So will this work? sure.
Is there a better way of doing this? Yes, absolutely. In general you don't want blocks of executable JS statements residing in a PHP script. This just muddles things up and makes them very tough to maintain. I would prefer to actually have JS request the value through an AJAX call to a PHP service. If you must use this approach, I would still separate concerns a little better.
EDIT: This works for me:
<html>
<head>
<title>PHP-JS Writer</title>
</head>
<body>
<?php
$a = "cat";
echo "<script>var b = '$a'</script>";
?>
<h1>Writing JS variables with PHP</h1>
<script>
alert(b);
</script>
</body>
</html>