Skip to content

Instantly share code, notes, and snippets.

Created Jun 6, 2012
What would you like to do?
Absurd variable names in Javascript and PHP
// 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';
// 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