7

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.

6
  • Yeah yeah along with all the other quirks in PHP and every other language written and spoken. Sorry what was the question? Feb 3, 2011 at 10:34
  • 2
    A nice variation of your second case is $b = 5...5; - though it constitutes more of a case where spaces would aid readability.
    – mario
    Feb 3, 2011 at 10:39
  • 2
    Does there exist a language where inserting spaces into the middle of tokens doesn't change behavior or break something? Feb 3, 2011 at 10:53
  • 1
    @Dan Grossman: C? int main(){ printf("Th" "is wi" "ll work!" ); }
    – mateusza
    Feb 3, 2011 at 10:56
  • In C, if you stick a space between a && operator, or between a ++ operator, it changes the behavior, right? Feb 3, 2011 at 11:07

3 Answers 3

10

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().

7
  • This happens all the time to me (so I know where to look), because AltGr+Space inserts an nbsp in my keyboard layout, so often I am not quick enough to release AltGr before pressing space, and here we go.
    – Mormegil
    Feb 3, 2011 at 10:58
  • 1
    Wow... Is there a bug filled for this? This surely does not follow what docs say: A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.
    – Mchl
    Feb 3, 2011 at 11:16
  • @Mchl I though about filing a bug report for that. Given how my previous bug reports where handled I decided not to. Interestingly enough, 0xA0 (the non-breaking space) fits the regular expression... Feb 3, 2011 at 11:23
  • 1
    Regarding the bug you're posted a link to: one of the greates flaws of PHP is that it lacks formal specification. This is the reason why bugs like this are filed under 'undefined behaviour, nothing to worry about'. Yes, now I see that 0xA0 indeed fits the regex. I'll nag guys from php doc team about it.
    – Mchl
    Feb 3, 2011 at 11:33
  • Bug? No - PHP (except for some dev builds) has always used ASCII, 0xA0 is an 8 bit char - and therefore not defined by this charset.
    – symcbean
    Feb 3, 2011 at 15:37
3

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.

1
  • 1
    Of course I do understand what is going on there. The question was what are the situations where omitting or placing whitespace affects the program (but both possibilities are correct).
    – mateusza
    Feb 3, 2011 at 13:36
0

In your example, you are breaking a single lexical token in two (a bit like breaking function into func tion, whitespace is indeed not ignored in that way). This is not really interesting, you could come up with many examples when things break that way, the only interesting task is to try to find an example which works (i.e. not a syntax error), but differently, e.g.:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.