Jump to content

Why should you learn javascript when you know php?


Bentley4

Recommended Posts

Hi everyone,

I understand that Js is clientside and php is serverside. But why would anyone want to do clientside programming when you can do serverside programming?

Could you give some examples from sites and say what is better to do in javascript and what is better to do in php?

 

Link to comment
Share on other sites

Hi everyone,

I understand that Js is clientside and php is serverside. But why would anyone want to do clientside programming when you can do serverside programming?

Could you give some examples from sites and say what is better to do in javascript and what is better to do in php?

 

It's not that one can do something better than the other, it's that each offers different capabilities, and each is used for different purposes.  PHP is a back end processing language.  As such, it's responsible for handling data requests (saving/retrieving data from a database) and showing the results of those requests.  JavaScript is a language primarily used to manipulate the Document Object Model (DOM) of an HTML page.  It's essentially used as a user interface scripting language, which allows sites to be more interactive.

 

The two languages are used together with AJAX, which allows you to send a request to your PHP script through JavaScript, and then process the return data with JavaScript, removing page refreshes from the process.  That is how apps like Google Maps work.

Link to comment
Share on other sites

But php is interactive as well, no? Is there anything in js that you couldn't actually write in php?

 

PHP is not interactive.  It has no concept of events, and cannot react to a user doing something to the rendered output.  All PHP does is respond to HTTP requests - GET or POST - and that requires a page load, either a refresh or a new load based on the user being redirected to another page.

Link to comment
Share on other sites

The answer to your original post cannot be put any better than Nightslyr's response

 

Is there anything in js that you couldn't actually write in php?

As you have stated yourself, JS is a client side language i.e it is executed by your web browser. PHP isn't, therefore 90% of the examples on the following page cannot be done with PHP:

 

http://www.noupe.com/jquery/50-amazing-jquery-examples-part1.html

Link to comment
Share on other sites

JS and PHP both serve a different need. JS is aimed at rich-media interaction while PHP is aimed at data processing and enforcing business rules. You can't enforce these constraints at the client's computer. That said though doesn't mean JS can't interact with a database, it sure can but it wouldn't be secure for the same reason you use session's instead of cookies.

Link to comment
Share on other sites

Javascript is very similar to php so why not learn it.

 

JavaScript is nothing like PHP. Syntax wise, maybe a little, but that is about where the similarities end. It has a completely different inheritance model based on prototypes.

Link to comment
Share on other sites

Javascript is very similar to php so why not learn it.

 

JavaScript is nothing like PHP. Syntax wise, maybe a little, but that is about where the similarities end. It has a completely different inheritance model based on prototypes.

 

Yes darling similar - not identical

 

Javascript

 

function test(txt){
if(txt){
alert("Txt is valid");
}else{
alert("no txt");
}
}//end test

 

Php

 

function test($txt){
if($txt){
echo "Txt is valid";
}else{
echo "no txt";
}
}//end test

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.