把內容以array的形式存入php檔案內,使用時直接require即可,該方式可達到高效緩存的目的
/** 把內容以array的形式存入php檔案內,使用時直接require即可,該方式可達到高效緩存的目的 獲取內容(1個參數): 参数: [0]:str型,PHP的路徑和檔案名的組合,如:Run.'_tip/_'.$uid.'.php' 說明: 返回php檔案內的array 用法: $x=Fun::PHP(Run.'_tip/_'.$uid.'.php'); 返回: array( 0=>bool型,是否執行成功 1=>若執行失敗則返回str型的錯誤消息,成功則返回array ) 新增內容(2個參數): 参数: [0]:str型,PHP的路徑和檔案名的組合,如:Run.'_tip/_'.$uid.'.php' [1]:未知,要新增的內容 說明: 把第2個參數的內容追加到$arr的尾巴上 用法: $x=Fun::PHP(Run.'_tip/_'.$uid.'.php',$arr); 返回: array( 0=>bool型,是否執行成功 1=>若執行失敗則返回str型的錯誤消息,成功則返回array ) 刪除內容(3個參數): 参数: [0]:str型,PHP的路徑和檔案名的組合,如:Run.'_tip/_'.$uid.'.php' [1]:str型,['del':只刪除參數[2]對應的一行;'delall':全刪] [2]:int型,要刪除的索引$key值 說明: 把第3個參數所屬的那一行內容刪除或全刪(要看[1]的值而定) 用法: $x=Fun::PHP(Run.'_tip/_'.$uid.'.php','del',$key); Fun::PHP(Run.'_tip/_'.$uid.'.php','delall',''); 返回: array( 0=>bool型,是否執行成功 1=>若執行失敗則返回str型的錯誤消息,成功則返回array ) /**/ public static function PHP(){ $o=func_get_args(); $c=count($o); if($c<1){return array(FALSE,'Error:'.__LINE__.','.basename(__FILE__).' 至少要傳遞一個參數!');} //確保副檔名是.php if(substr($o[0],-4)!='.php'){return array(FALSE,'Error:'.__LINE__.','.basename(__FILE__).' 副檔名必須是.php');} $t='<?PHP'.PHP_EOL.'//該檔案由系統自動產出請勿手工修改'.PHP_EOL; //若暫存檔不存在則創建之 if(!file_exists($o[0])){ file_put_contents($o[0],$t.'$arr=array();',LOCK_EX); chmod($o[0],0777); } require($o[0]); $arr=(!is_array($arr)) ? array() : $arr; switch($c){ case 1: //傳遞了1個參數(0=>php檔案名),返回php檔案內的array return array(TRUE,$arr); case 2: //傳遞了2個參數(0=>php檔案名,1=>要新增的內容),把第2個參數的內容追加到$arr的尾巴上 $arr[]=$o[1]; $arr=$t.'$arr='.var_export($arr,TRUE).';'; file_put_contents($o[0],$arr,LOCK_EX); chmod($o[0],0777); unset($o,$t); return array(TRUE,$arr); case 3: //傳遞了3個參數(0=>php檔案名,1=>'del',2=>要刪除的索引$key值),把第3個參數所屬的那一行內容刪除 if(!in_array($o[1],array('del','delall'))){return array(FALSE,'Error:'.__LINE__.','.basename(__FILE__).' 參數2錯誤!');} switch($o[1]){ case 'del': //把第3個參數所屬的那一行內容刪除 if(!isset($o[2]) or !is_numeric($o[2])){return array(FALSE,'Error:'.__LINE__.','.basename(__FILE__).' 參數3錯誤!');} if(!isset($arr[$o[2]])){return array(TRUE,$arr);} //若要刪除的項目已不存在則直接返回 unset($arr[$o[2]]); $arr=$t.'$arr='.var_export($arr,TRUE).';'; file_put_contents($o[0],$arr,LOCK_EX); chmod($o[0],0777); unset($o,$t); return array(TRUE,$arr); case 'delall': //全刪 if(!isset($o[2])){return array(FALSE,'Error:'.__LINE__.','.basename(__FILE__).' 參數3錯誤!');} $arr=$t.'$arr=array();'; file_put_contents($o[0],$arr,LOCK_EX); chmod($o[0],0777); unset($o,$t); return array(TRUE,array()); } throw new exception('Error:'.__LINE__.','.basename(__FILE__).'!');die(); default: unset($o,$t); return array(FALSE,'Error:'.__LINE__.','.basename(__FILE__).' 參數個數錯誤!'); } unset($o,$t); throw new exception('Error:'.__LINE__.','.basename(__FILE__).'!');die(); }
以上就是把內容以array的形式存入php檔案內,使用時直接require即可,該方式可達到高效緩存的目的的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment