You know what are the differences between the control structure in Javascript and control structure in PHP? Both use the if / else, for or while, but that vary in that change?
Are two different programming languages but there are differences in the part of "control structure" in the syntax although both are similar.
Examples
Javascript control structure
<script type="text/javascript">
var a = 5;
var b = 8;
if (a < b)
{
alert(a +" is smaller than " +b);
}
</script>
PHP control structure
<?php
// Number comparison
$a = 5;
$b = 8;
if ($a < $b)
{
echo $a ."is smaller than" .$b;
}
// Result : 5 is smaller than 8
?>
Thanks
Öhm, there are many big differences. The most important thing is that JavaScript
runs on client side through your browser and PHP
runs on server side. For PHP you need a webserver with a PHP module to understand that type of script.
PHP code stays on the server. If you ran your PHP side the server will send you the through PHP generated HTML. The JavaScript code will be send together with this HTML to the client/Browser and after it it will be called...
With PHP you can read your Database. With JavaScript (client-side) you can make an AJAX call to your server where a PHP script reads your Database and returns the data. Update: But JavaScript could also run server-side and then read your Database.
PHP is often used to generate HTML. JavaScript is often used to manipulate/animate HTML.
In this case, there is no difference. Both operands are numeric (JS Number
type, PHP int
's). The difference is JS's type coercion, and how certain values are represented internally (all numbers are 64bit IEEE-754 floats, PHP distinguished between floats (which are actually doubles) and ints (which are actually of the C type long
). Both languages use lazy evaluation (if (false && <expression2>)
will not evaluate the second expression). Google the PHP truth tables so see some special oddities like "0"
being false, whereas '0'
is true.
Check the PHP truth tables here.
Note that the idea that JS is restricted to run client-side is just plain wrong. JS is a very small, highly portable language. it runs server-side (node.js), it runs on DB's (mongoDB, couchbase), it is used as a scripting language in other programs (most adobe software), on domestic appliances like smart-TV's... its the most common programming language in the world.
This is because JavaScript does not have IO capabilities (IO methodologies differ depending on how/where the code runs). To generate output, JS uses API's. In a browser, it uses the DOM API, which is not gouverned by ECMA, but by W3C. It's not part of JavaScript.