Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: whitespaces that do matter [closed]

Whitespaces are generally ignored in PHP syntax, but there are several places where you cannot put them without affecting the sense (and the result).

One example is:

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

Do you know of any other examples?

The question is not about why is that working this way, but what are situations, when omitting or placing whitespace changes the program, but both versions are syntactically correct.

like image 211
mateusza Avatar asked Feb 03 '11 10:02

mateusza


People also ask

Does PHP ignore whitespace?

Whitespaces are generally ignored in PHP syntax, but there are several places where you cannot put them without affecting the sense (and the result).

How do I cut space between words in PHP?

The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string. rtrim() - Removes whitespace or other predefined characters from the right side of a string.

How do I check if a string contains only spaces in PHP?

A ctype_space() function in PHP is used to check whether each and every character of a string is whitespace character or not. It returns True if the all characters are white space, else returns False.

What does PHP do with whitespace?

The ctype_space() function in PHP check for whitespace character(s). It returns TRUE if every character in text creates some sort of white space, FALSE otherwise. Besides the blank character this also includes tab, vertical tab, line feed, carriage return and form feed characters.


2 Answers

That one had me going berserk. I present the whitespace of doom:

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

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

When executing, the error was:

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

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

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

Notice the double spaces:

function  foo()

It turned out that by copy/pasting the above code (formatted with highlight_string), a non-breaking space  , or 0xA0 is acceptable in identifiers, so the function call was to 0xA0foo() instead of foo().

like image 159
Linus Kleen Avatar answered Oct 19 '22 20:10

Linus Kleen


You can't put spaces in the middle of a number! Gosh!

$x = 10 3.5; //syntax error

If you put a space in the middle of an operator, it's no longer that operator!!

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

If I put a space in the middle of my string, it's not the same string!

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

Sorry, but this question is ridiculous, since of course you can't throw spaces in the middle of tokens without either changing behavior or breaking code. Just the same as in virtually every other programming and written language.

5.5 is a number, 5 . 5 is a string because . is the string concatenation operator. That's just the language's syntax.

like image 34
Dan Grossman Avatar answered Oct 19 '22 20:10

Dan Grossman