revjim.net Rotating Header Image

PHP converts periods to underscores

In case you didn't know it, PHP converts periods (".") to underscores ("_") in all variable names. This includes GET, POST and COOKIE variables. A period is not a valid character in a PHP variable name. If you consider the syntax you would have to use in order to access it, you can see why:

$foo.bar = 'hello';
if($foo.bar == 'hello') {
    $foo.baz = $foo.bar;
}

The period is a concatenate operator in PHP. Therefore, in these constructs, PHP is going to attempt to regard everything after the period as another variable, or as a constant, or as something that needs to be evaluated and then concatenated with the value of $foo. I'm sure, if it were really needed, PHP could implement a special construct to access these (${foo.bar} perhaps). But, if you didn't realize you had to convert the period in the variable name to an underscore, you probably also wouldn't realize that you had to use a special construct in order to access it, so, even if this construct did exist, it would defeat its own purpose, I think. [Thanks Keith]

Google Buzz
blog comments powered by Disqus