function drupal_eval

6.x common.inc drupal_eval($code)

Evaluate a string of PHP code.

This is a wrapper around PHP's eval(). It uses output buffering to capture both returned and printed text. Unlike eval(), we require code to be surrounded by <?php ?> tags; in other words, we evaluate the code as if it were a stand-alone PHP file.

Using this wrapper also ensures that the PHP code which is evaluated can not overwrite any variables in the calling code, unlike a regular eval() call.

Parameters

$code: The code to evaluate.

Return value

A string containing the printed output of the code, followed by the returned output of the code.

2 calls to drupal_eval()
block_list in drupal-6.x/modules/block/block.module
Return all blocks in the specified region for the current user.
php_filter in drupal-6.x/modules/php/php.module
Implementation of hook_filter(). Contains a basic PHP evaluator.

File

drupal-6.x/includes/common.inc, line 1726
Common functions that many Drupal modules will need to reference.

Code

function drupal_eval($code) {
  global $theme_path, $theme_info, $conf;

  // Store current theme path.
  $old_theme_path = $theme_path;

  // Restore theme_path to the theme, as long as drupal_eval() executes,
  // so code evaluted will not see the caller module as the current theme.
  // If theme info is not initialized get the path from theme_default.
  if (!isset($theme_info)) {
    $theme_path = drupal_get_path('theme', $conf['theme_default']);
  }
  else {
    $theme_path = dirname($theme_info->filename);
  }

  ob_start();
  print eval('?>' . $code);
  $output = ob_get_contents();
  ob_end_clean();

  // Recover original theme path.
  $theme_path = $old_theme_path;

  return $output;
}