Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we should use PHP?

Tags:

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

like image 986
narayanpatra Avatar asked Nov 28 '10 17:11

narayanpatra


2 Answers

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.

like image 51
Emil Vikström Avatar answered Sep 23 '22 10:09

Emil Vikström


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.

like image 27
Ivo Wetzel Avatar answered Sep 24 '22 10:09

Ivo Wetzel