Skip to content

Instantly share code, notes, and snippets.

@Zegnat
Created February 4, 2011 17:11
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 Zegnat/811387 to your computer and use it in GitHub Desktop.
Save Zegnat/811387 to your computer and use it in GitHub Desktop.
CASSIS: Scooping
// Javascript needs you to declare variables inside functions or they will be created in the global scoop.
$var1 = 'global';
function change() {
if (js()) eval('var $var1;');
$var1 = 'scooped';
}
change();
// PHP needs you to declare variables inside functions if you want them to be in the gobal scoop.
$var2 = 'global';
function change2() {
if (!js()) eval('global $var2;');
$var2 = 'scooped';
}
change2();
@Zegnat
Copy link
Author

Zegnat commented Feb 4, 2011

To think about when writing CASSIS code:

Keep within a function.

Without line 4 $var1 will equal global in PHP and scooped in Javascript. When line 4 is included $var1 equals global in both cases. The value from inside the function no longer leaks into the global scoop.

Accessing global scoop.

Without line 12 $var2 will equal global in PHP and scooped in Javascript. When line 12 is included $var2 equals scooped in both cases. The value from inside the function correctly overwrites the global scoop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment