Skip to content

Instantly share code, notes, and snippets.

@timw4mail
Created June 6, 2012 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timw4mail/2884414 to your computer and use it in GitHub Desktop.
Save timw4mail/2884414 to your computer and use it in GitHub Desktop.
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';
<?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