Hello. Please tell me if I am right. Interested in more PHP, is it possible to do so, are there any pitfalls.

Variable in JS need to declare

var a = "1"; a += 1; alert(a); // '11' 

This will not work

 a += 1; alert(a); 

And everything works in PHP

 $a .= 1; echo $a; // '1' 

JS does not work

 var a = '1'; if (a == '0') {b = '3';} alert(b); 

So show that the variable does not exist.

 var a = '1'; if (a == '0') {var b = '3';} alert(b); // undefined 

I declare a variable before if

 var a = '1', b = ''; if (a == '0') {b = '3';} alert(b); // '' 

But it works

 var a = '1'; if (a == '1') {var b = '3';} alert(b); // '3' 

And everything works in PHP

 $a = '1'; if ($a == '1') {$b = '3';} echo $b; // '3' 

And when the variable is not declared, an empty value is returned, not undefined.

 $a = '1'; if ($a == '0') {$b = '3';} if ($b == '') echo "Hellow"; // "Hellow" 
  • you have a few different examples .... for example in js write if (a == '0') {b = '3';} that is, compare with zero, although a declared equal to 1 ..... and in PHP if ($a == '1') {$b = '3';} while $a = 1; ........ but after all in js if to compare with 1 then the alert will output 3 as in PHP ....... in the first example with var a = "1"; also in js you have one written in PHP and another ..... but if you write $a = "1"; $a .= 1; echo $a; $a = "1"; $a .= 1; echo $a; also bring 11 .......... or so necessary? - Alexey Shimansky
  • if ($b == '') echo "Hellow"; // "Hellow" if ($b == '') echo "Hellow"; // "Hellow" here is undefined , just need to check === it checks the type - L. Vadim
  • Variables should always be declared. The simplest rake: if, as shown in the PHP examples, variables are not declared, and the code is in, say, a procedure, then instead of implicitly declaring a local variable, you can crash the value of the variable global. And the program will be very grateful to you ... - Akina

2 answers 2

Because php takes over declarations of variables, making it opaque, invisible to the user. They are declared in the current context. For example:

 function test(){ $a = 'test passed'; } test(); var_dump($a);// >>> NULL 

JS , on the contrary, requires explicit definition of variables, however, without requiring explicitly specifying their type. Moreover, declaring a variable anywhere in the code raises it to the beginning of the current context, for example:

 function a(){ console.log(a); var a = 'test'; } //будет аналогично записи function b(){ var a; console.log(a); a = 'test'; } 

Variables in both languages ​​are declared in current contexts, and php does not look for variables in the context above (only if the global keyword is not used, which allows the use of variables from the global context):

 //php $a = 'test'; function test(){ var_dump($a);// >>> NULL $a = 'test passed'; echo $a;// >>> 'test passed' } test(); echo $a;// >>> 'test' 

However, js "searches" for a declaration of a variable in contexts, from the most nested to the most general, and if it does not find it, it sends a message with an error.

 //js var a = 'test'; var b = 'test N2'; function hax(){ a = 'test passed'; var b = 'test N2 passed'; function c(){ var a = 'test has been failed!'; console.log(a, b) //>>>'test has been failed', 'test N2 passed' } c(); } hax(); console.log(a, b);// >>> 'test passed', 'test N2' 
  • This is the difference in the process of declaring and accessing variables, and in fact it is variable in PHP, in C, in Java, and in other PLs. Variable is a container for storing program variable data. - Naumov
  • Although I did not understand the threat to the global variable. After all, it should be denoted global. It turns out that in PHP you need to declare a global variable, and the local one will be up from the loop and from the condition - Natalya Mitrofanova
  • Hmmm, that's true. So far, I’ll remove this item from the answer, maybe afterwards @Akina will explain to us what the matter is, mb I profileil somewhere - SLy_huh

In order to check the existence of a variable, you need to do if (isset ($ b))