Brief Introduction to PHP eval () function definition and Usage _php instance

Source: Internet
Author: User
Tags function definition parse error

The eval () function calculates the string according to the PHP code.

The string must be a valid PHP code and must end with a semicolon.

Returns NULL if the return statement is not invoked in the code string. If there is a parse error in the code, the eval () function returns FALSE.

Grammar

 
 

Parameters Description
Phpcode Necessary. Specify the PHP code to be computed.

Tips and comments

Note: The return statement immediately terminates the calculation of the string.

Note: This function is useful for storing code in database text fields for future calculations.

Example

<?php
$string = "Beautiful";
$time = "Winter";
 
$str = ' is a $string $time morning! ';
echo $str. "<br/>";
 
Eval ("\ $str = \" $str \ ";");
echo $str;
? > 

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 defined dynamically according to the configuration of the system, and the specific code fragment is as follows:

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

The above article on the PHP eval () function definition and usage is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.