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.
Whitespaces are generally ignored in PHP syntax, but there are several places where you cannot put them without affecting the sense (and the result).
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.
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.
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.
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()
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With