PHP: spaces that matter

Simple spaces are usually ignored in PHP syntax, but there are several places where you cannot put them without affecting the meaning (and the result).

One example:

$a = 5.5;    // five and a half (float)
$b = 5 . 5;  // "55" (string)

Do you know about other examples?

The question is not about why this works , but in situations where dropping or placing spaces changes the program, but both versions are syntactically correct.

+5
source share
3 answers

This one made me berserk. I represent the gap of doom:

function foo() {
   print "I'm foo.";
}

if (something()) {
    foo();
}

When executing the error:

Fatal error: function call undefined foo () on line 6

After an hour or so, I found out that the error message actually said:

Fatal error: Call to undefined function  foo() on line 6

:

function  foo()

, / ( highlight_string),  , 0xA0 , 0xA0foo() foo().

+8

! !

$x = 10 3.5; //syntax error

, !!

if (true & & true) echo 'true'; //syntax error

, !

echo "Hel lo World"; //does NOT print "Hello World"!

, , , , , . , .

5.5 - , 5 . 5 - , . - . .

+2

In your example, you split one lexical token into two parts (a bit, like splitting functioninto func tion, gaps are not really ignored in this way). This is not very interesting, you can come up with a lot of examples when the situation is violated, the only interesting task is to try to find an example that works (i.e. Not a syntax error), but in a different way, for example:

$a = $x++ + $y;   // x is incremented
$b = $x+ + + $y;  // x is not incremented
0
source

All Articles