开发者

Why we should use PHP?

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开发者_JS百科 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. JavaScript is run client-side.

You can for example not do anything database related in 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. 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. 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).

They are two entirely different languages, made for different needs.

There are however a few JS servers, which run JavaScript code server-side. Node.JS is 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.


The main difference here is not PHP 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.

JavaScript is an embeddable scripting language, so it can run in a Browser, but it can just as well run on a Server.

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

Saying that JavaScript is client-side these days, is like saying that Lua is only uses for scripting games.


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.

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.


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.


The biggest difference is that PHP runs on the server while JavaScript runs on the client.

Think of the server and client as two people, say Alice and Bob, and consider the following two scenarios:

  • Alice bakes a cake and sends it to Bob. This is like server-side processing using 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 processing using JavaScript. Your browser gets the JavaScript source code from the server and executes it on your computer.

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.


Whatever we can do by PHP, can be done using Javascript(I think that but I am a noob to this).

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.


PHP and JavaScript are both just languages. 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. 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.

The client-side limitation remains the same, however. That is because browsers typically only implement a JavaScript runtime.


In addition to not being able to access a database, you wouldn't want to 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 is a client-side scripting 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-side scripting language. That is, it executes on the web server, and has access to the resources available on the web server computer.

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.


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.

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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜