📜 ⬆️ ⬇️

Difference Javascript from PHP

For some reason, my articles are generally perceived as articles for beginners, but, if anything, I try to write for everyone, and, moreover, I focus more on knowledgeable people than on beginners :). Therefore, do not give in to the title: perhaps you can find something useful for yourself.

In the article, I would like to talk about some cool moments that a developer may encounter in some dynamic language when he sees PHP, or vice versa, a developer in PHP, when he learns some other dynamic languages. I do not consider static languages, because there seems to be so clear.

The author, what is the topic? Who needs your PHP?


The reason I decided to write this topic is simple: I would like to enlighten people who do not write in PHP (or write very little in PHP) a little bit about some interesting points in this language, which are pretty little spoken about. I don’t want to convince anyone that PHP is better than Python, Ruby, Javascript, [substitute your favorite language for the web here]: moreover, I don’t think so. I’m just “offended by the power” when PHP is run over, simply claiming, without argument, that this is a bad language.

Interesting PHP Differences from Javascript


As a reference dynamic language, I will take JS, because, most likely, everyone knows it, who writes for the web. Well, for those who write in PHP and do not know JS, this article probably can be useful in the sense that it will help to better understand Javascript.
')
Everything, except objects - values

PHP has many built-in types, and none of them behaves like an object. Built-in types have no properties, methods, etc. In this regard, PHP resembles the usual C. This plays a role when we move on to the following statement:

By default, everything is passed by value, and objects too.

In PHP, they tried to build into absolute the concept of the C language, that everything is transmitted by value. And they initially did it even for objects, but then changed the concept a bit - objects are still values, but this value is a link to the instance class .

What does this mean?

Let's look at a simple example:

PHP:
<?php
$arr = array(
'key1' => 'value1',
'key2' => 'value2',
);

function doSmthWithArray($arr) {
$arr['key3'] = 'value3';
}

doSmthWithArray($arr);
print_r($arr); //  "key1 => value1, key2 => value2" —    


Javascript:
var arr = {
key1: 'value1',
key2: 'value2'
};

function doSmthWithArray(arr) {
arr['key3'] = 'value3';
}

doSmthWithArray(arr);
console.log(arr); //  "key1 => value1, key2 => value2, key3 => value3" — ..  JS,  ,    «»,   ,      (,    —   . ,            ,      ,    ) 


, , , . , , , .

PHP:
<?php
$funcs = array();
for($i = 0; $i < 10; $i++) $funcs[] = function() use($i) { return $i; };

foreach($funcs as $func) echo $func().",";
//  0, 1, 2, 3, 4, 5, 6, 7, 8, 9 !

$funcs = array();
for($i = 0; $i < 10; $i++) $funcs[] = function() use(&$i) { return $i; };

foreach($funcs as $func) echo $func().",";
//  10, 10, 10, 10, 10, 10, 10, 10, 10, 10

:)? !

Javascript:
var funcs = [];
for(var i = 0; i < 10; i++) funcs.push(function() { return i; });
for(var j = 0; j < funcs.length; j++) console.log(funcs[j]());
//  10   10


, , JS, . PHP PHP, , .


PHP , "$var .= something;" PHP — , . JS , «var += something» .

, PHP : [.]. :
<?php
$str = 3.'3'; // 33
$another_str = 3 + '3'; // 6


Javascript [ 3 + '3' ] 33. . , . .

Copy-on-write

. : PHP , , , . , , , , . , . .

, PHP , jQuery, $.extend().

PHP -

, , JS: -, , , length …

, PHP

PHP , JS: , . : var JavaScript :).


, , , . , PHP PHP, , , , .

Source: https://habr.com/ru/post/122803/


All Articles