Summary of the usage of eval function in PHP

Source: Internet
Author: User
    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 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 configuration of the system, with specific code snippets:

    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 value of a variable in a string, and is usually used to manipulate the data on the database. The parameter code_str is the string to be processed. Note: The string to be processed conforms to the PHP string format, with a semicolon at the end. The string that is processed with this function ends with the continuation 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.