Javascript 为什么要使用PHP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4298078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why we should use PHP?
提问by narayanpatra
I have just started to learn HTML, JavaScript and PHP. After studying little, I sometimes think to myself, "why we need PHP"? Whatever we can do using PHP, can be done using Javascript(I think that but I am a noob to this). So why do we use PHP? Can anybody explain to me its use?( I apologize in advance if the question is totally foolish and the answer is very obvious - but as I said, I am noob to web).
我刚刚开始学习 HTML、JavaScript 和 PHP。很少学习之后,我有时会想,“为什么我们需要 PHP”?无论我们可以使用 PHP 做什么,都可以使用 Javascript 来完成(我认为,但我对此是个菜鸟)。那么我们为什么要使用PHP呢?任何人都可以向我解释它的用途吗?(如果问题完全愚蠢并且答案非常明显,我提前道歉 - 但正如我所说,我是网络新手)。
回答by Emil Vikstr?m
PHP is a server-side scripting language. JavaScript is run client-side.
PHP 是一种服务器端脚本语言。JavaScript 在客户端运行。
You can for example not do anything database related in JavaScript. Even ifthere where database libraries written for JavaScript they would be of no use because your server cannot trust database queries done by the client. For example, you cannot delete forum posts client-side because then anybody would be able to delete those posts under the disguise of an administrator.
例如,您不能在 JavaScript 中执行任何与数据库相关的操作。即使如果那里有对JavaScript编写的数据库库,他们将是没有用的,因为你的服务器不能信任数据库查询由客户端来完成。例如,您不能在客户端删除论坛帖子,因为这样任何人都可以伪装成管理员删除这些帖子。
PHP can do a lotthat JS cannot do. Image resizing, saving files on the server, database queries, e-mailing, PDF generation, secure login systems, RSS parsing, SOAP calls to web services, anything where you cannot trust the client (because the user can change JS code as they want, and there's no way for you to control that).
PHP可以做很多JS做不到的事情。图像调整大小、在服务器上保存文件、数据库查询、电子邮件、PDF 生成、安全登录系统、RSS 解析、对 Web 服务的 SOAP 调用、任何您不信任客户端的事情(因为用户可以根据需要更改 JS 代码) ,并且您无法控制它)。
They are two entirely different languages, made for different needs.
它们是两种完全不同的语言,用于满足不同的需求。
There are however a few JS servers, which run JavaScript code server-side. Node.JSis one example of such a system. Then you don't need to learn a new language, but you still need to differentiate what you do in the client from what you do on the server-side.
然而,有一些 JS 服务器在服务器端运行 JavaScript 代码。Node.JS就是这种系统的一个例子。那么你不需要学习一门新的语言,但你仍然需要区分你在客户端做什么和你在服务器端做什么。
回答by Ivo Wetzel
The main difference here is notPHP vs. JavaScript. It's Client vs. Server. You can't do server stuff on the client, and you can't do all the Client stuff on the Server.
这里的主要区别不是PHP 与 JavaScript。这是Client vs. Server。你不能在客户端做服务器的事情,你也不能在服务器上做所有的客户端的事情。
JavaScript is an embeddablescripting language, so it can run in a Browser, but it can just as wellrun on a Server.
JavaScript是一种可嵌入的脚本语言,因此它可以在浏览器中运行,但它可以一样好一个服务器上运行。
So all you need is to run it on a Server and there's no more need for PHP, how do we do that?
Node.js
所以你所需要的只是在服务器上运行它并且不再需要 PHP,我们如何做到这一点?
节点.js
Saying that JavaScript is client-sidethese days, is like saying that Lua is only uses for scripting games.
现在说 JavaScript 是客户端,就像说 Lua 只用于脚本游戏。
回答by bmb
Despite the fact that PHP is commonly written in-line in the same files as HTML and Javascript, PHP is a server side language. Javascript is a client-side language.
尽管 PHP 通常在与 HTML 和 Javascript 相同的文件中内嵌编写,但 PHP 是一种服务器端语言。Javascript 是一种客户端语言。
For a self-described "noob", I can think of no better place to start your education than to understand the difference between code that runs on the server and on the client.
对于一个自称是“菜鸟”的人,我认为没有比了解在服务器上运行的代码和在客户端上运行的代码之间的区别更好的方式来开始您的教育了。
回答by robvleugel
PHP is designed as server-side scripting language, JS for client-side. Sure, you can use JS for server-side as well if you really wanted to. You can also learn German rather then Dutch if you're moving to the Netherlands since quite a lot of people speak it, its just not the most efficient way of communicating. Sure, its easier to use a single language rather then having to learn both PHP and JS. Same is true for German, which is spoken by more people in the world, but when staying in the Netherlands for a long time it pays off to know the Dutch language. If you are planning to only make a single website, then sticking to JS alone might be the best thing to do. If on the other hand you are planning to be a serious web developer, its worthwhile to know PHP eventually anyway. Learning both is time well spent I say.
PHP 被设计为服务器端脚本语言,JS 被设计为客户端。当然,如果您真的愿意,您也可以将 JS 用于服务器端。如果你搬到荷兰,你也可以学习德语而不是荷兰语,因为很多人都说德语,这不是最有效的交流方式。当然,使用单一语言比同时学习 PHP 和 JS 更容易。德语也是如此,德语在世界上被更多人使用,但在荷兰长期居住时,了解荷兰语是值得的。如果你打算只做一个网站,那么坚持使用 JS 可能是最好的选择。另一方面,如果您打算成为一名认真的 Web 开发人员,那么无论如何最终了解 PHP 是值得的。我说学习两者都是值得的。
回答by casablanca
The biggest difference is that PHP runs on the server while JavaScript runs on the client.
最大的区别是 PHP 在服务器上运行,而 JavaScript 在客户端运行。
Think of the server and client as two people, say Alice and Bob, and consider the following two scenarios:
将服务器和客户端视为两个人,比如 Alice 和 Bob,并考虑以下两个场景:
Alice bakes a cake and sends it to Bob. This is like server-side processingusing PHP. Bob just gets the cake and has no idea who baked it or how. Similarly, in your browser, all you see is the final output generated by PHP, you don't see the "source code".
Alice sends the ingredients and recipe to Bob, and Bob bakes the cake at his house. This is like client-side processingusing JavaScript. Your browser gets the JavaScript source code from the server and executes it on your computer.
爱丽丝烤了一个蛋糕并发送给鲍勃。这就像使用 PHP 的服务器端处理。鲍勃刚拿到蛋糕,不知道是谁烤的,也不知道是怎么烤的。同样,在您的浏览器中,您看到的只是 PHP 生成的最终输出,您看不到“源代码”。
爱丽丝将配料和食谱发送给鲍勃,鲍勃在他家烤蛋糕。这就像使用 JavaScript 的客户端处理。您的浏览器从服务器获取 JavaScript 源代码并在您的计算机上执行它。
Now you get the same end-result in both cases, however this may not always be the case:
现在,您在两种情况下都会得到相同的最终结果,但情况可能并非总是如此:
Security:Alice may not want Bob to know her secret recipe, so she always bakes the cake and sends it. This is like using PHP to hide the details of what happens on the server.
Resources:Bob may not have the necessary tools (eg. an oven) to bake the cake, so Alice does it for him. This is like using PHP to connect to a database because JavaScript does not have access to the database server.
安全性:Alice 可能不想让 Bob 知道她的秘方,所以她总是烤蛋糕然后寄出去。这就像使用 PHP 来隐藏服务器上发生的事情的细节。
资源:Bob 可能没有必要的工具(例如烤箱)来烤蛋糕,所以 Alice 为他做。这就像使用 PHP 连接数据库一样,因为 JavaScript 无法访问数据库服务器。
回答by Darin Dimitrov
Whatever we can do by PHP, can be done using Javascript(I think that but I am a noob to this).
无论我们可以通过 PHP 做什么,都可以使用 Javascript 来完成(我认为,但我对此是个菜鸟)。
No this is not true. One thing you cannot do with javascript is to connect to a SQL database on your server which is kind of common task in web development.
不,这不是真的。使用 javascript 无法做的一件事是连接到服务器上的 SQL 数据库,这是 Web 开发中的常见任务。
回答by Stéphan Kochen
PHP and JavaScript are both just languages. As mentioned by the other answers, they are commonly usedfor server-side and client-side scripting respectively.
PHP 和 JavaScript 都只是语言。正如其他答案所提到的,它们通常分别用于服务器端和客户端脚本。
However, there's a twist, which should be mentioned. JavaScript can actually be used server-side as well. There are server runtime environments such as node.jsthat allow you do that, and potentially allow you to get away with one less programming language to use.
然而,有一个转折,应该提到。JavaScript 实际上也可以用于服务器端。有诸如node.js 之类的服务器运行时环境允许您这样做,并且可能允许您使用更少的编程语言来摆脱困境。
The client-side limitation remains the same, however. That is because browsers typically only implement a JavaScript runtime.
但是,客户端限制保持不变。这是因为浏览器通常只实现 JavaScript 运行时。
回答by Daniel Pryden
JavaScript is a client-sidescripting language. That is, it executes in the client (web browser in this instance) and has access to resources on the client computer (such as the actual keyboard and mouse inputs, etc.). PHP is a server-sidescripting language. That is, it executes on the web server, and has access to the resources available on the web server computer.
JavaScript 是一种客户端脚本语言。也就是说,它在客户端(本例中为 Web 浏览器)中执行,并且可以访问客户端计算机上的资源(例如实际的键盘和鼠标输入等)。PHP 是一种服务器端脚本语言。也就是说,它在 Web 服务器上执行,并且可以访问 Web 服务器计算机上可用的资源。
Other combinations are possible:
其他组合也是可能的:
- Some web browsers support client-side scripting in languages other than JavaScript. (For instance, Internet Explorer allows VBScript.) But JavaScript (or, more formally, ECMA-262 ECMAScript) is the only client-side scripting language supported out of the box on all modern web browsers.
- It's possible to use PHP with clients that aren't web browsers. For example, a PHP script can produce XML instead of HTML, and that XML could be read by an RSS reader, for instance.
- Depending on your web server configuration, there are many more choices for server-side development than just PHP. These range from the "enterprisey" solutions like Java EE and ASP.NET to lightweight languages like PHP and Perl, and everything in between.
- 某些 Web 浏览器支持使用 JavaScript 以外的语言编写的客户端脚本。(例如,Internet Explorer 允许使用 VBScript。)但 JavaScript(或更正式地说,ECMA-262 ECMAScript)是唯一一种在所有现代 Web 浏览器上都支持开箱即用的客户端脚本语言。
- 可以将 PHP 与不是 Web 浏览器的客户端一起使用。例如,PHP 脚本可以生成 XML 而不是 HTML,并且该 XML 可以由 RSS 阅读器读取。
- 根据您的 Web 服务器配置,服务器端开发有更多选择,而不仅仅是 PHP。这些范围从 Java EE 和 ASP.NET 等“企业”解决方案到 PHP 和 Perl 等轻量级语言,以及介于两者之间的一切。
回答by Vasanth
Both PHP and javascript are scripting languages. But php is bound to the server side and javascript is bound to the client side. A user need not do anything if he/she wants to use a PHP site because the server hosting the php application puts up with the computing involved in it. As far as the user is concerned it's just plain HTML.
PHP 和 javascript 都是脚本语言。但是php绑定到服务器端,javascript绑定到客户端。如果用户想要使用 PHP 站点,则他/她不需要做任何事情,因为托管 php 应用程序的服务器可以承受其中涉及的计算。就用户而言,它只是普通的 HTML。
But in case of Javascript the computation that is requried to process the javascript requests is handled by the user's own processor, plus there is another issue of having enabled javascript in that particular person's browser. With php there is no such issue. So php is better. I hope I helped. Good luck...
但是在 Javascript 的情况下,处理 javascript 请求所需的计算由用户自己的处理器处理,另外还有另一个问题是在该特定人的浏览器中启用了 javascript。使用 php 就没有这样的问题。所以php更好。我希望我有所帮助。祝你好运...
回答by mpen
In addition to not being able to access a database, you wouldn't wantto do everything client-side with JavaScript. It would be too much processing for the client; especially those trying to run your site on older computers or on mobile devices. Using PHP, you can pre-compute the HTML before sending it to the client.
除了无法访问数据库之外,您也不希望在客户端使用 JavaScript 完成所有操作。对于客户端来说,这将是太多的处理;尤其是那些试图在较旧的计算机或移动设备上运行您的网站的人。使用 PHP,您可以在将 HTML 发送到客户端之前预先计算它。