-
- $string = "beautiful";
- $time = "winter";
- $str = 'This is a $string $time morning!';
- echo $str. "
";
- eval("$str = "$str";");
- echo $str;
- ?>
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:
-
-
- if ( ! isset($active_record) OR $active_record == TRUE)
- {
- require_once(BASEPATH.'database/DB_active_rec.php');
- if ( ! class_exists('CI_DB'))
- {
- eval('class CI_DB extends CI_DB_active_record { }');
- }
- }
- else
- {
- if ( ! class_exists('CI_DB'))
- {
- eval('class CI_DB extends CI_DB_driver { }');
- }
- }
- require_once(BASEPATH.'database/drivers/'.$params[' dbdriver'].'/'.$params['dbdriver'].'_driver.php');
- // Instantiate the DB adapter
- $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
- $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.
|