Printing PHP variables into JavaScript variables

javascript, php

Solution

Use this no need to give "" => change to '.$test1.'..

<?php
 $test1 = '1';

 print '
 <script type="text/javascript">
      var carnr;        
      carnr = "'.$test1.'"
      console.log(carnr);
 </script>';
 ?>

Problem

Yes I know this question gets asked a lot, but I'm fairly new to JS and I need to use a php variable in some JS. I'm more then aware that PHP is executed server side and JS is client side however other people claim that this works. I've got a PHP variable called "test1" that I want to log to the JS console (for instance): ``` <?php $test1 = '1'; print ' <script type="text/javascript"> var carnr; carnr = "<?php print($test1); ?>" console.log(carnr); </script>'; ?> ``` What this does is print " " to the JS console. Not exactly what I was hoping for. Now this may not even be doable and I may have to pass the variable off the page and back in again with AJAX, but I'd rather have a quick and easy solution if there is one available! Any help is appreciated.

Original source

Related problems