Difference between JavaScript and PHP - GeeksforGeeks

18 Apr, 2022
In this article, we will know about Javascript & PHP, along with understanding their significant differences.
A long time before, most 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 frameworks 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.
JavaScript: It is the most popular lightweight, interpreted compiled programming language. It is also known as a scripting language for web pages. It is well-known for the development of web pages, many non-browser environments also use it. JavaScript can be used for Client-side developments as well as Server-side developments.
Syntax: 
<script>
    document.write("JavaScript syntax");
</script>
You can place the tags, anywhere within your web page, but it is normally recommended to keep it within the <head> tag. This tag tells the browser to start interpreting all the text between these tags as JavaScript code. 
Example 1: This example describes the Javascript basic example to print the text for the specified time using the for loop.
HTML
<!DOCTYPE html><html><title>Basic JavaScript Example</title><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>
Output:
A simple JavaScript program
GeeksforGeeks 
GeeksforGeeks 
GeeksforGeeks 
GeeksforGeeks 
GeeksforGeeks 
Example 2: This example illustrates Javascript getElementById() method where element_id is used to get the text.
HTML
<!DOCTYPE html><html><title>Javascript Example</title><style>    #gfg {        font-size: 40px;        color: #006400;    }</style> <body>    <h2 id="gfg">GeeksforGeeks</h2>    <button onclick="myFunction()">Click Me!</button>    <script>        function myFunction() {            document.getElementById("gfg").innerHTML = "Hello Geeks!";        }    </script></body> </html>
Output:
Getting the element using getElementById Method
PHP: PHP is an acronym for Hypertext Preprocessor, which is a server-side scripting language designed specifically for web development. PHP can be easily embedded in HTML files and HTML codes can also be written in a PHP file. Like Javascript, PHP can also be written in HTML code and in the .php file extension itself too. But it requires a server to run, so you won’t be able to see an output of the code. in a simple manner.
Syntax:
<?php
    echo "Hello Geeks!!!";
?> 
Steps to run the PHP code: 
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 to htdocs folder of xampp. Now, open xampp, run apache, and SQL server, & now go to localhost with your file URL (type localhost/folder name/filename.php or localhost/filename.php in your browser) and there you can see the output. PHP code starts with <?php and ends with ?>. This is to tell the compiler/server that the PHP language starts here.  
Example 1: This example describes the PHP for loop to display the repeated output.
HTML
<!DOCTYPE html><html><title>PHP Code inside the HTML</title><body>    <?php       // Declare variables using $ symbol     $str= "GeeksforGeeks";     $x = 5;      // PHP for loop     for( $i = 0; $i< $x; $i++ ) {          echo ("GeeksforGeeks");     }     ?>    <!--This code will print GeeksforGeeks 5 times on front end--></body> </html>
Output:
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
Difference between Javascript vs PHP:
thor98571
bhaskargeeksforgeeks
itskawal2000
JavaScript-Misc
JavaScript
PHP
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
How to execute PHP code using command line ?
How to Insert Form Data into Database using PHP ?
PHP in_array() Function
How to convert array to string in PHP ?
How to pop an alert message box using PHP ?