Skip to content Skip to sidebar Skip to footer

Best Way To Incorporate Javascript In Php?

This is the way I am currently doing it. \n'; echo 'alert('Congrats');\n'; echo ''; ?> Is th

Solution 1:

Just put your JavaScript code outside PHP tags:

<?php// your PHP code goes here?>// your javascript function out of the PHP tag.functionf() {
   alert('congrats');
}

Solution 2:

of course

?>
alert('Congrats');
<?

Solution 3:

If you really have to execute the js by printing it from the PHP, it would at least be cleaner if you had your js functionality stored in functions in some file and then called them by printing the calls.

Solution 4:

I recommend reserving PHP files just for PHP code and keeping your frontend code (HTML/CSS/javascript) in separate template files.

Last time I checked, mixing the presentation layer & backend code into same file was popular about 12 years ago.

Your file hierarchy for a project could look like this:

- my_project
   - lib
      - PHP files here
   - templates
      - HTML templates here
   - public <- thisis your document root for web server
     - index.php <- just a dispatcher
     - js
     - images
     - css

Solution 5:

Use HEREDOCS, or break out of PHP mode into "html" mode. If the Javascript is entirely static, or has a few parts that need to have some PHP value included, drop into html mode ('?>' out of php). This will allow any decent text editor to realize that you're doing HTML and Javascript, and syntax highlight as appropriate. The following are all equivalent, but decide for yourself which is more readable:

'pure php':

<?phpecho'<script>';
   echo'  var x = [' . $somePHPvar . '];';
   echo'  alert(x);';
   echo'<script>';
?>

'heredoc' syntax:

<?phpecho<<<EOF
    <script>
       var x = [{$somePHPvar}];
       alert(x);
    </script>
EOF;
?>

'html mode':

<?php?><script>var x = [<?phpecho$somePHPVar?>];
    alert(x);
</script>

plusses/minuses for each:

  1. pure php: you can stay in PHP mode, and your echo + $vars will be highlighted as PHP code, but the html/javascript you're echoing will be treated as plain text and colored as such (ie: all the same color)
  2. heredoc syntax: You stay in PHP mode, but gain the benefit of not having to escape any quotes (' and ") in your code, so any html will look cleaner. Most editors will recognize PHP vars in the heredoc block and color them appropriately, but the rest of the text will be treated as text, so javascript/html look the same. Also, you cannot insert function calls into the text. You have to do those BEFORE starting the heredoc and store the results in a var, which can be inserted. The HEREDOC can also be use to assign long text blocks into a variable directly.
  3. 'html mode': The editor will see/recognize your html, javascript, AND php and color them appropriately. But this is at the cost of having to sprinkle php open/close tags anywhere you need to fill in some value dynamically. On the plus side, you can directly insert function call results (htmlspecialchars(), urlecncode(), html_strip_tags(), etc...) without having to store the values in an intermediate var. It also makes for harder-to-maintain code as your PHP is now sprinkled randomly throughough the html/javascript code.

It all boils down to what's easiest for the particular code you're working on.

Post a Comment for "Best Way To Incorporate Javascript In Php?"