简体   繁体   English

我们为什么要使用PHP?

[英]Why we should use PHP?

I have just started to learn HTML, JavaScript and PHP. 我刚开始学习HTML,JavaScript和PHP。 After studying little, I sometimes think to myself, "why we need PHP"? 在学习不多之后,我有时会想,“为什么我们需要PHP”? Whatever we can do using PHP, can be done using Javascript(I think that but I am a noob to this). 无论我们使用PHP做什么,都可以使用Javascript(我想,但我是一个菜鸟)。 So why do we use PHP? 那么我们为什么要使用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). 任何人都可以向我解释它的用途吗?(如果这个问题完全是愚蠢的,而且回答非常明显,我会提前道歉 - 但正如我所说,我是网络的通奸者)。

PHP is a server-side scripting language. PHP是一种服务器端脚本语言。 JavaScript is run client-side. JavaScript在客户端运行。

You can for example not do anything database related in JavaScript. 例如,您可以不在JavaScript中执行任何与数据库相关的操作。 Even if there where database libraries written for JavaScript they would be of no use because your server cannot trust database queries done by the client. 即使那里有数据库库的JavaScript编写的,他们将是没有用的,因为你的服务器无法信任客户端进行数据库查询。 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. 例如,您无法删除客户端的论坛帖子,因为任何人都可以在管理员的伪装下删除这些帖子。

PHP can do a lot that JS cannot do. PHP可以做很多 JS无法做到的事情。 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). 图像调整大小,保存服务器上的文件,数据库查询,电子邮件,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. 然而,有一些JS服务器,它们运行JavaScript代码服务器端。 Node.JS is one example of such a system. Node.JS就是这种系统的一个例子。 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. 然后,您不需要学习新语言,但仍需要区分您在客户端中执行的操作与在服务器端执行的操作。

The main difference here is not PHP vs. JavaScript. 这里的主要区别不是 PHP与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. 您无法在客户端上执行服务器操作,也无法在服务器上执行所有客户端操作。

JavaScript is an embeddable scripting language, so it can run in a Browser, but it can just as well run 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? 所以你需要的只是在服务器上运行它并且不再需要PHP,我们如何做到这一点?
Node.js Node.js的

Saying that JavaScript is client-side these days, is like saying that Lua is only uses for scripting games . 现在说JavaScript是客户端 ,就像说Lua仅用于脚本游戏

Despite the fact that PHP is commonly written in-line in the same files as HTML and Javascript, PHP is a server side language. 尽管PHP通常在与HTML和Javascript相同的文件中在线编写,但PHP是一种服务器端语言。 Javascript is a client-side language. 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. 对于一个自我描述的“noob”,我认为没有比学习服务器和客户端上运行的代码之间的差异更好的地方开始你的教育。

PHP is designed as server-side scripting language, JS for client-side. PHP被设计为服务器端脚本语言,JS用于客户端。 Sure, you can use JS for server-side as well if you really wanted to. 当然,如果你真的想要,也可以在服务器端使用JS。 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. 当然,它更容易使用单一语言,而不是必须学习PHP和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. 如果您打算只创建一个网站,那么坚持使用JS可能是最好的选择。 If on the other hand you are planning to be a serious web developer, its worthwhile to know PHP eventually anyway. 另一方面,如果您计划成为一名认真的Web开发人员,那么最终还是值得了解PHP。 Learning both is time well spent I say. 学习两者都是我说的好时光。

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 processing using PHP. 这就像使用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". 同样,在浏览器中,您看到的只是PHP生成的最终输出,您没有看到“源代码”。

  • Alice sends the ingredients and recipe to Bob, and Bob bakes the cake at his house. 爱丽丝将食材和食谱送给鲍勃,鲍勃在家里烤蛋糕。 This is like client-side processing using JavaScript. 这就像使用JavaScript的客户端处理 Your browser gets the JavaScript source code from the server and executes it on your computer. 您的浏览器从服务器获取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. 这就像使用PHP隐藏服务器上发生的事情的细节。

  • Resources: Bob may not have the necessary tools (eg. an oven) to bake the cake, so Alice does it for him. 资源: Bob可能没有必要的工具(例如烤箱)来烤蛋糕,所以Alice为他做了。 This is like using PHP to connect to a database because JavaScript does not have access to the database server. 这就像使用PHP连接数据库一样,因为JavaScript无权访问数据库服务器。

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开发中的一项常见任务。

PHP and JavaScript are both just languages. PHP和JavaScript都只是语言。 As mentioned by the other answers, they are commonly used for server-side and client-side scripting respectively. 正如其他答案所述,它们通常分别用于服务器端和客户端脚本。

However, there's a twist, which should be mentioned. 然而,有一个扭曲,应该提到。 JavaScript can actually be used server-side as well. JavaScript实际上也可以在服务器端使用。 There are server runtime environments such as node.js that allow you do that, and potentially allow you to get away with one less programming language to use. 有一些服务器运行时环境,例如node.js ,允许您这样做,并且可能允许您使用少一种编程语言。

The client-side limitation remains the same, however. 然而,客户端限制保持不变。 That is because browsers typically only implement a JavaScript runtime. 这是因为浏览器通常只实现JavaScript运行时。

In addition to not being able to access a database, you wouldn't want to do everything client-side with JavaScript. 除了无法访问数据库之外,您不希望使用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. 使用PHP,您可以在将HTML发送到客户端之前预先计算HTML。

JavaScript is a client-side scripting language. JavaScript是一种客户端脚本语言。 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.). 也就是说,它在客户端(在此实例中为Web浏览器)中执行,并且可以访问客户端计算机上的资源(例如实际的键盘和鼠标输入等)。 PHP is a server-side scripting language. PHP是一种服务器端脚本语言。 That is, it executes on the web server, and has access to the resources available on the web server computer. 也就是说,它在Web服务器上执行,并且可以访问Web服务器计算机上可用的资源。

Other combinations are possible: 其他组合是可能的:

  • Some web browsers support client-side scripting in languages other than JavaScript. 某些Web浏览器支持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. (例如,Internet Explorer允许使用VBScript。)但JavaScript(或更正式地说,ECMA-262 ECMAScript)是所有现代Web浏览器中支持开箱即用的唯一客户端脚本语言。
  • It's possible to use PHP with clients that aren't web browsers. 可以将PHP与非Web浏览器的客户端一起使用。 For example, a PHP script can produce XML instead of HTML, and that XML could be read by an RSS reader, for instance. 例如,PHP脚本可以生成XML而不是HTML,例如,RSS阅读器可以读取XML。
  • Depending on your web server configuration, there are many more choices for server-side development than just PHP. 根据您的Web服务器配置,服务器端开发有很多选择,而不仅仅是PHP。 These range from the "enterprisey" solutions like Java EE and ASP.NET to lightweight languages like PHP and Perl, and everything in between. 其中包括Java EE和ASP.NET等“企业级”解决方案,PHP和Perl等轻量级语言,以及介于两者之间的所有内容。

Both PHP and javascript are scripting languages. PHP和JavaScript都是脚本语言。 But php is bound to the server side and javascript is bound to the client side. 但是php绑定到服务器端,javascript绑定到客户端。 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. 如果用户想要使用PHP站点,则用户不需要做任何事情,因为托管php应用程序的服务器会涉及到它所涉及的计算。 As far as the user is concerned it's just plain HTML. 就用户而言,它只是简单的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. 但是在Javascript的情况下,处理javascript请求所需的计算由用户自己的处理器处理,此外还有另一个问题是在该特定人的浏览器中启用了javascript。 With php there is no such issue. 使用PHP没有这样的问题。 So php is better. 所以php更好。 I hope I helped. 我希望我帮忙。 Good luck... 祝好运...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM