Home > Backend Development > PHP Tutorial > Summary of usage of php's eval function

Summary of usage of php's eval function

WBOY
Release: 2016-07-25 09:03:32
Original
1208 people have browsed it
  1. $string = "beautiful";
  2. $time = "winter";
  3. $str = 'This is a $string $time morning!';
  4. echo $str. "
    ";
  5. eval("$str = "$str";");
  6. echo $str;
  7. ?>
Copy code

output: This is a $string $time morning! This is a beautiful morning winter!

The

eval() function is also used in the CodeIgniter framework. In the /system/database/DB.php file, a class CI_DB is dynamically defined according to the system configuration. The specific code snippet:

  1. if ( ! isset($active_record) OR $active_record == TRUE)
  2. {
  3. require_once(BASEPATH.'database/DB_active_rec.php');
  4. if ( ! class_exists('CI_DB'))
  5. {
  6. eval('class CI_DB extends CI_DB_active_record { }');
  7. }
  8. }
  9. else
  10. {
  11. if ( ! class_exists('CI_DB'))
  12. {
  13. eval('class CI_DB extends CI_DB_driver { }');
  14. }
  15. }
  16. require_once(BASEPATH.'database/drivers/'.$params[' dbdriver'].'/'.$params['dbdriver'].'_driver.php');
  17. // Instantiate the DB adapter
  18. $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
  19. $DB = new $driver($params);
Copy code

This function can substitute the variable value in the string and is usually used to process database data. The parameter code_str is the string to be processed. Note: The string to be processed must conform to PHP's string format and must have a semicolon at the end. The string processed using this function will be continued until the end of the PHP program.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template