Computer >> Computer tutorials >  >> Programming >> Javascript

Difference Between PHP and JavaScript


In this post, we will understand the differences between PHP and JavaScript −

JavaScript

  • It helps work with frond end as well as back end
  • It is asynchronous, which means it doesn't wait for the input and output operations.
  • It can be run in browsers and since 'Node' has been released, JavaScript can also run be run on the command line.
  • It can be combined with HTML, AJAX and XML.
  • It is a single threaded language which is event-driven. This means it doesn't block everything and runs concurrently.
  • The statements are placed within the <script> and </script> tags.
  • These tags can be present anywhere within the webpage, but it is recommended to keep them within the <head> tags.
  • The <head> tag tells the browser to start the interpretation of text within these tags as JavaScript code.

A sample JavaScript code −

Example

<!DOCTYPE html>
<html>
<body>
<h2> JavaScript code</h2>
<script>
   var n;
   n=2;
   for(var i=0; i<n; i++){
      document.write("Hi " +"<br>");
   }
</script>
</body>
</html>

Output

JavaScript code
Hi
Hi

PHP

  • It can be written in HTML code, as well as within the .php file.
  • PHP requires a server to run.
  • XAMPP or a local server application can be installed to run PHP.
  • The code in PHP starts with <?php and ends with ?>.
  • This tells the server that PHP code begins here.

A sample PHP code −

Example

<!DOCTYPE html>
<html>
<body>
<h1>PHP Code </h1>
<?php
   $str= "Hi";
   $x = 2;
   for( $i = 0; $i<2; $i++ ) {
      echo ("Hi");
   }
?>
</body>
</html>

Output

Hi
Hi

Conclusion

In this post, we understood about the significant differences between PHP and Javascript.