You are here

function php_eval in PHP 8

Evaluates 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.

This function is also used as an implementation of hook_filter_FILTER_process().

Parameters

string $code: The code to evaluate.

Return value

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

See also

php_filter_info()

2 calls to php_eval()
Php::evaluate in src/Plugin/Condition/Php.php
Evaluates the condition and returns TRUE or FALSE accordingly.
Php::process in src/Plugin/Filter/Php.php
Performs the filter processing.

File

./php.module, line 64
Additional filter for PHP input.

Code

function php_eval($code) {

  /* FIXME global $theme_path, $theme_info;

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

    // Restore theme_path to the theme, as long as php_eval() executes,
    // so code evaluated will not see the caller module as the current theme.
    // If theme info is not initialized get the path from default theme.
    if (!isset($theme_info)) {
      $theme_path = drupal_get_path('theme', Drupal::config('system.theme')->get('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;
}