Created
June 6, 2012 20:09
-
-
Save timw4mail/2884414 to your computer and use it in GitHub Desktop.
Absurd variable names in Javascript and PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In javascript, object properties that are not valid for the dot syntax can be defined with square brackets. | |
// Similar to PHP: | |
obj[' '] = 'foo'; | |
// While there isn't a direct method to create a variable with a invalid name, | |
// you can fake it using the document/window variable, or this | |
this[' '] = 'bar'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// In PHP, variables that are not valid like $var-name, can be defined as ${'var-name'}. | |
// Therefore: | |
${' '} = 'foo'; | |
// Objects behave similarly: | |
$obj->{' '} = 'bar'; | |
// Array keys don't have these issues, and behave like the ${} construct: | |
$array[' '] = 'baz'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment