PHP eval() 函數

來源:互聯網
上載者:User
文章目錄
  • 文法
定義和用法

eval() 函數把字串按照 PHP 代碼來計算。

該字串必須是合法的 PHP 代碼,且必須以分號結尾。

如果沒有在代碼字串中調用 return 語句,則返回 NULL。如果代碼中存在解析錯誤,則 eval() 函數返回 false。

文法
eval(phpcode)

  

參數 描述
phpcode 必需。規定要計算的 PHP 代碼。
 提示和注釋

注釋:返回語句會立即終止對字串的計算。

注釋:該函數對於在資料庫文字欄位中供日後計算而進行的代碼儲存很有用。

例子
<?php$string = "beautiful";$time = "winter";$str = 'This is a $string $time morning!';echo $str. "<br />";eval("\$str = \"$str\";");echo $str;?>   

輸出:

This is a $string $time morning!

This is a beautiful winter morning! 


eval() 函數在CodeIgniter架構裡也有用到。在 /system/database/DB.php 檔案中,根據系統的配置動態定義了一個類 CI_DB,具體程式碼片段如下:
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);

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.