Skip to main content
added 202 characters in body
Source Link
Christoph
  • 1.5k
  • 9
  • 20

PHP

While the other PHP submissions only test if the given value matches a value of a global, this version works by taking a reference to the value:

// take a reference to the global variable
function f(&$i){
  foreach(array_reverse($GLOBALS) as $key => $value)
    if($key != 'GLOBALS') {
      // setSet allthe Globalsvalue toof theireach ownglobal to its name 
 (doesn't work vor $GLOBALS)  $GLOBALS[$key] = $key;
    }
    
  foreach($GLOBALS as $key => $value)
    if($key != 'GLOBALS') $GLOBALS[$key]&& $key != $key;$value) {
      // outputThe thevalues referencedmismatch so it had a reference to another value
      // we delete it
      unset(which$GLOBALS[$key]);
 is now    // and update the value to its name oragain
 still an array)   $GLOBALS[$key] = $key;
    }

  echo '$', is_array($i) ? 'GLOBALS' : $i;$i, "\n";
}

This works as long asshould now work even if the global variable is not a reference to another value at the time of calling.

Test it herehere.

PHP

While the other PHP submissions only test if the given value matches a value of a global, this version works by taking a reference to the value:

// take a reference to the global variable
function f(&$i){
  // set all Globals to their own name (doesn't work vor $GLOBALS)
  foreach($GLOBALS as $key => $value)
    if($key != 'GLOBALS') $GLOBALS[$key] = $key;
  // output the referenced value (which is now the name or still an array)
  echo '$', is_array($i) ? 'GLOBALS' : $i;
}

This works as long as the global variable is not a reference to another value at the time of calling.

Test it here.

PHP

While the other PHP submissions only test if the given value matches a value of a global, this version works by taking a reference to the value:

// take a reference to the global variable
function f(&$i){
  foreach(array_reverse($GLOBALS) as $key => $value)
    if($key != 'GLOBALS') {
      // Set the value of each global to its name 
      $GLOBALS[$key] = $key;
    }
    
  foreach($GLOBALS as $key => $value)
    if($key != 'GLOBALS' && $key != $value) {
      // The values mismatch so it had a reference to another value
      // we delete it
      unset($GLOBALS[$key]);
      // and update the value to its name again
      $GLOBALS[$key] = $key;
    }

  echo '$', is_array($i) ? 'GLOBALS' : $i, "\n";
}

This should now work even if the global variable is a reference to another value at the time of calling.

Test it here.

Source Link
Christoph
  • 1.5k
  • 9
  • 20

PHP

While the other PHP submissions only test if the given value matches a value of a global, this version works by taking a reference to the value:

// take a reference to the global variable
function f(&$i){
  // set all Globals to their own name (doesn't work vor $GLOBALS)
  foreach($GLOBALS as $key => $value)
    if($key != 'GLOBALS') $GLOBALS[$key] = $key;
  // output the referenced value (which is now the name or still an array)
  echo '$', is_array($i) ? 'GLOBALS' : $i;
}

This works as long as the global variable is not a reference to another value at the time of calling.

Test it here.