Difference between JavaScript and Php

A long time before, most of the people used to think Php is a server side language and Javascript as client side language as it was only executed in web browsers. But after V8, Node and other framework came, Javascript is capable of doing a lot of things Php used to. Since, we can handle both front-end and back-end through Javascript now, It’s considered as more powerful than Php. Here are some of the difference between Javascript and Php.

Javascript Php
Does job for Both Front-end and Back-end. Php is used mostly for Back-end Purposes only.
Javascript is asynchronous, It doesn’t wait for Input Output operations. Php is synchronous, It waits for IO operations to execute.
Can be run in browsers and after Node, we can also run it in Command line3. Php requires a Server to Run. Cannot run without a server.
Js can be combined with HTMl, AJAX and XML. Can be combined with HTML only.
It is a single threaded language that is event-driven which means it never blocks and everything runs concurrently. It is multi-threaded which means it blocks I/O to carry out multiple tasks concurrently.

JavaScript syntax and program

JavaScript statements are placed within the <script>… </script> tags.
You can place the tags, anywhere within your web page, but it is normally recommended to keep it within the <head> tags. The tag tells the browser to start interpreting all the text between these tags as JavaScript code.
Syntax:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
  
<script>
<!-- your javascript code here -->
  
document.write("GeeksforGeeks");
</script>
  
</body>
</html>

chevron_right


Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- To print a text five times-->
<!DOCTYPE html>
<html>
<body>
<h2>A simple JavaScript program</h2>
  
<script>
  
   var n;
   n=5;
     
   //JavaScript uses the var keyword to declare variables.
   //An equal sign is used to assign values to variables.
  
   for(var i=0; i<n; i++){
   document.write("GeeksforGeeks " +"<br>");
   }
     
</script>
</body>
</html>                    

chevron_right


Output:

A simple JavaScript program
GeeksforGeeks 
GeeksforGeeks 
GeeksforGeeks 
GeeksforGeeks 
GeeksforGeeks 

Code #2:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
<h2 id="gfg" style="font-size:40px; color:#006400;">
                                  GeeksforGeeks</h2>
<button onclick="myFunction()">Click Me!</button>
<script>
  
    function myFunction() {
        document.getElementById("gfg").innerHTML = "Hello Geeks!";
    }
</script>
</body>
</html>       

chevron_right


Output:
Before clicking “Click Me!” button-

After clicking “Click Me!” button-

PHP syntax and basic program:
Like Javascript, Php can also be written in HTML code and in .php file itsef too. But Php requires a server to run, so you won’t be able to see an output of the following code.
Instructions to Run Php:
You can install Xampp or any other local server app. After installing Xampp, name your code file with the extension .php and move your Html or Php file in Htdocs folder of xampp. Open xampp, run apache and Sql server, now go to local host with your file URL (type local/yourfilename.php in your browser) and there you can see it.
Php code starts with <?php and ends with ?>. This to tell the compiler/server that Php language starts here.
Syntax:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
  
<h1>PHP Code in HTML</h1>
  
<?php
echo "Hello Geeks!!!";
?> 
<!-- Echo to print on front end-->
  
</body>
</html>

chevron_right


Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
<h1>PHP Code in HTML</h1>
<?php
   
   // in php we declare variables using $
   $str= "GeeksForGeeks";
   $x = 5;
     
   //for loop in php
   for( $i = 0; $i<5; $i++ )
   {
        echo ("GeeksForGeeks");
        }
          
?> 
<!--This code will print GeeksforGeeks 5 times on front end-->
</body>
</html>

chevron_right


Output:

GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.