Summary of php eval function usage

Source: Internet
Author: User
Summary of php eval function usage

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

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

The eval () function is also useful 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 is as follows:

  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 );

This function can substitute variable values in strings and is usually used to process database data. The code_str parameter is the string to be processed. Note: the string to be processed must conform to the PHP string format and contain a semicolon at the end. The strings processed by using this function are continued to the end of the PHP program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.