Difference Between JavaScript and PHP

Difference between Javascript and PHP

What is the differene b/w php and javascript

Roughly akin to the difference between English and German. They can express largely the same things, but do so in different ways, and you'll have more luck using English in Germany then German in England.

i know one is server side scripting and the other is browser side

Not really.

PHP is a programming language. It is often used for server side programming, but has uses in general programming too.

JavaScript is a programming language. It is the only language that has a decent level of native support for running in a browser. It has a wide variety of server side implementations (including Node and ASP). It is one of the languages you can use with the Windows Scripting Host. etc.

There are plenty of other languages that can be used for server side web programming too (C# is popular in ASP.NET, I'm rather fond of Perl, there are quite a lot of proponents of Python and Ruby, Java has a strong following, and so on).

That said. El Cheapo hosting which supports PHP is a lot more common than El Cheap hosting which supports other things. Leaving language partisanship aside, the primary disadvantage with it is that El Cheapo hosting is has the You Gets What You Pay For rule.

If we take your question to be about the difference between server side and client side programming though…

but what m asking is that using client side programming i can display alert messages

With client side programming you can manipulate things in the browser without going back to the server. e.g. you can add elements to the document to display a message.

You also have access to APIs provided by the browser, such as the alert() method which will display a message box that isn't an intrinsic part of the document and Local Storage (which lets you store data in the browser which only that browser will have access to).

You can make HTTP requests to ask the server for things (this is called Ajax).

which i can simply do with server side programming also,without using any function

With server side programming, you can modify the document you are sending to the client, but only at load time.

You can access shared resources (such as the contents of a database that lives on the server).

You don't have access to things like the alert() method. (Although you can generate program code (usually in JS) that will run client side and will have access to those methods).

so does server side and client side programming are exclusive ,like if i use one then the other one should not be used,or ??

In general, any essential functionality should be handled with server side programming. Build on things that work. Client side programming can break, either because you depend on a feature that isn't available in the browser the user is using, because a script fails to load, because the user happens to have JavaScript turned off, or because the user is trying something malicious (such as passing data to the server that could cause an XSS or SQL injection problem).

Client side programming, on the other hand, can be used to make things more convenient for the user. You can add animation to indicate that something is happening, check data before it is submitted to the server (saving the time of a round trip), update part of a page periodically, and so on.

Difference between how php and javascript execute a nested function

Its a scope thing. You could have as easily - in javascript - written "var b = function()". "b" is just a variable of type function defined within the scope of the function a. In PHP, both "a" and "b" are global functions, but it's the job of function "a" to define "b", so it won't get defined until "a" is called. Consider this example...

function a($x) {
if ($x) {
function b() { echo "x not empty"; }
} else {
function b() { echo "x empty"; }
}
}
a(1); // Defines function b
b(); // x not empty
a(0); // PHP Fatal error: Cannot redeclare b() (previously declared...

You can see by the failure to redefine "b", that "b" is a real, globally scoped function. Function "a" could use various criteria to define the function for a particular purpose in different runs. Clearly, in this case, it wouldn't make sense to call function "b" before function "a" has decided how to define it.

I don't, by the way, think the example above is very good coding practice, but it does serve to illustrate the point.

The PHP code most similar to your javascript code would be:

function a() {
$b = function() {
echo "'b' says inner";
};
$b(); // Demonstrating the function can be used inside "a"
}
a(); // 'b' says inner

$b is a variable of type function, which can only be used within function "a".

Javascript cookies vs php cookies

HTTP Cookies are not a feature of PHP, nor a feature of Javascript : those are just programming languages that allow a developper to manipulate them.

The biggest difference between JS and PHP is that :
  • Javascript runs on the client side
  • PHP runs on the server side

But cookies are still the same : they are defined as a standard -- see RFC 2965.

Still, note that modern browsers implement cookies that are not accessible from Javascript *(see the `httponly` option of [`setcookie`][3])* -- which means that, depending on the browser, and the way a cookie was set, it might not be accessible from Javascript.

This is a security measure -- and is not a difference between "js cookies" and "php cookies" : it's just a property of some cookies.

PHP and JavaScript - What is the difference?

PHP runs on the server.

Javascript runs within the browser (what's called the client).

They are completely distinct. You can do a website without Javascript; however, any in-browser "scripts" will not run with PHP being given to the browser, since the browser does not understand PHP.

So think about it like this...

  1. You create a PHP page on your server, called mypage.php.
  2. Within it contains PHP code.
  3. The PHP code is parsed on the server when accessed over HTTP (http://example.com/mypage.php).
  4. Which sends the output, or HTML and other "resources" (Javascript and CSS mostly), to the browser (client).

Javascript would be part of the response to the browser requesting the mypage.php content. On the server, HTML/Javascript/CSS are considered a special kind of text, and have no bearing on the PHP code itself. PHP is meant to ease the management of outputting HTML/Javascript/CSS to browsers, mostly.

A More Extensive Answer

Server - mypage.php

<html>
<head>
<style type="text/css">
body p {
background-color: #dddddd;
color: #ff0000;
}
</style>
<script type="text/javascript">

alert('Thank you for visiting!');

</script>
</head>
<body>
<p><?php print 'Hello World!'; ?></p>
</body>
</html>

What the browser (client) sees - http://example.com/mypage.php

<html>
<head>
<style type="text/css">
body p {
background-color: #dddddd;
color: #ff0000;
}
</style>
<script type="text/javascript">

alert('Thank you for visiting!');

</script>
</head>
<body>
<p>Hello World!</p>
</body>
</html>

Notice the script tags stay as they are, but the PHP tags go away. They are parsed by the PHP parser on the server-side, before outputting the HTML code (with style and script tags intact) to the browser (client).

Why is there a difference between JavaScript and PHP timestamp

PHP is executed on the server side, and in your example, JavaScript works on the client-side.

Both sides have their own time configuration. For the server, time zone settings etc. will stay the same (unless you change them), but the server has no idea which time zone the current visitor is in. There’s no way for you to control that.

If I change my system clock on my laptop, it will influence client-side JavaScript date/time, but your server timer won’t be affected.

Difference between PHP and JS evaluation of variables

Is it just the way things were implemented in the language?

Yes, JavaScript does it a bit differently. The expression (something || 321) means if something is of a falsy value, a default value of 321 is used instead.

In conditional expressions || acts as a logical OR as usual, but in reality it performs the same coalescing operation. You can test this with the following:

if ((0 || 123) === true)
alert('0 || 123 evaluates to a Boolean');
else
alert('0 || 123 does not evaluate to a Boolean');

In PHP the || operator performs a logical OR and gives a Boolean result, nothing else.



Related Topics



Leave a reply



Submit