PHP and JavaScript - What is the difference?
What is the big difference between PHP and JavaScript?
Can we do website only with PHP (and HTML) or is it absolutely necessary you n开发者_如何学运维eed JavaScript too?
PHP runs on the server
.
Javascript runs within the browser
(what's called the client
).
They are completely distinct. You can do a website without Javascript; however, any in-browser "scripts" will not run with PHP being given to the browser, since the browser does not understand PHP.
So think about it like this...
- You create a PHP page on your server, called mypage.php.
- Within it contains PHP code.
- The PHP code is parsed on the server when accessed over HTTP (http://example.com/mypage.php).
- Which sends the output, or HTML and other "resources" (Javascript and CSS mostly), to the browser (client).
Javascript would be part of the response to the browser requesting the mypage.php content. On the server, HTML/Javascript/CSS are considered a special kind of text, and have no bearing on the PHP code itself. PHP is meant to ease the management of outputting HTML/Javascript/CSS to browsers, mostly.
A More Extensive Answer
Server - mypage.php
<html>
<head>
<style type="text/css">
body p {
background-color: #dddddd;
color: #ff0000;
}
</style>
<script type="text/javascript">
alert('Thank you for visiting!');
</script>
</head>
<body>
<p><?php print 'Hello World!'; ?></p>
</body>
</html>
What the browser (client) sees - http://example.com/mypage.php
<html>
<head>
<style type="text/css">
body p {
background-color: #dddddd;
color: #ff0000;
}
</style>
<script type="text/javascript">
alert('Thank you for visiting!');
</script>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Notice the script tags stay as they are, but the PHP tags go away. They are parsed
by the PHP parser on the server-side, before outputting the HTML code (with style and script tags intact) to the browser (client).
PHP is a server-side scripting language which allows you to produce your HTML programmatically, interacting with a database and/or other services in the process. You upload your PHP files and any related resources to a webserver that supports PHP and whenever a browser requests a page, the PHP code it contains is executed by the server and the HTML it outputs is returned to the browser for the browser to display to the user. This HTML may contain references to images, CSS files and/or Javascript files (or not - this is entirely optional depending on the requirements).
Javascript is a client-side scripting language that executes in the browser and has no direct access to your database. It does not care what language or operating system is in use on the server. Javascript files reside on your webserver alongside your PHP pages, but when a browser requests them, the server does not execute them as it does with the PHP code, it simply passes them straight to the browser, just as it would an image. The browser then executes the code on the user's machine (which is why they cannot talk directly to your databases etc - there is no direct connection from the user's machine to your database).
Javascript is not a requirement for a website, although it is commonly used due to the benefits it can provide. PHP is not your only choice for the server-side code either, it is just one of a number of different programming languages you can use.
(This answer has been updated based on further information provided by the original poster)
Javascript
1: Originally created for be executed in the web browser, however it is also possible to execute Javascript in the server side using NodeJS.
2: Javascript is based in the Ecmascript specifications.
3: It is pure functional language however it is possible to emulate many OOP features like classes using a transpiler or using a version of NodeJS that supports the ES5 specification.
4: Originally was created by Netscape as a solution for DOM manipulation, and web browser scripting.
5: Javascript is asynchronous by nature, what it means that it was designed in order to compute without wait for IO operations such network requests, file operations (With NodeJS), etc. It makes Javascript ideal for low latency solutions like stream servers.
6: Javascript is most used for develop web site front-ends, mobile apps, desktop apps, webservices and stream servers.
PHP
1: It was originally created for be executed in the server side and it is still used as server side programming language.
2: It was originally inspired in C language, however it evolved to a some kind of OOP language with C as root language.
3: It is a OOP language since version 4, and today (PHP 7.1) it has many modern OOP features like classes, inheritance, polymorphism, overloading, encapsulation, constructors, destructors…
4: It was originally created by Ramus Lerdorf with the purpose of develop their personal home page (PHP).
5: PHP is synchronous by nature, what it means that it waits for IO operations, that it makes easy to develop programs with the step by step approach. (Note that is possible to develop asynchronous PHP apps using extensions like Swoole, in fact in some benchmarks it can be faster than Javascript running under NodeJS).
6: PHP is most used for develop web site back-ends and webservices.
You should read http://en.wikipedia.org/wiki/PHP and http://en.wikipedia.org/wiki/Javascript
PhP is server-side, whereas javascript is client-side.
You don't absolutely need javascript, it is basically used for ui effects and dynamisation.
PHP is usually used for dynamic pages creation. Before the user will see them. Javascript is usually used for response to events and dynamic change of a content without page reload.
You can easily make a website without JavaScript... And without PHP, too.
精彩评论