search
Homephp教程PHP源码把內容以array的形式存入php檔案內,使用時直接require即可,該方式可達到高效緩存的目的

把內容以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,&#39;Error:&#39;.__LINE__.&#39;,&#39;.basename(__FILE__).&#39; 至少要傳遞一個參數!&#39;);}

	//確保副檔名是.php
	if(substr($o[0],-4)!=&#39;.php&#39;){return array(FALSE,&#39;Error:&#39;.__LINE__.&#39;,&#39;.basename(__FILE__).&#39; 副檔名必須是.php&#39;);}

	$t=&#39;<?PHP&#39;.PHP_EOL.&#39;//該檔案由系統自動產出請勿手工修改&#39;.PHP_EOL;

	//若暫存檔不存在則創建之
	if(!file_exists($o[0])){
		file_put_contents($o[0],$t.&#39;$arr=array();&#39;,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.&#39;$arr=&#39;.var_export($arr,TRUE).&#39;;&#39;;
		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=>&#39;del&#39;,2=>要刪除的索引$key值),把第3個參數所屬的那一行內容刪除
		if(!in_array($o[1],array(&#39;del&#39;,&#39;delall&#39;))){return array(FALSE,&#39;Error:&#39;.__LINE__.&#39;,&#39;.basename(__FILE__).&#39; 參數2錯誤!&#39;);}

		switch($o[1]){
		case &#39;del&#39;:		//把第3個參數所屬的那一行內容刪除
			if(!isset($o[2]) or !is_numeric($o[2])){return array(FALSE,&#39;Error:&#39;.__LINE__.&#39;,&#39;.basename(__FILE__).&#39; 參數3錯誤!&#39;);}

			if(!isset($arr[$o[2]])){return array(TRUE,$arr);} //若要刪除的項目已不存在則直接返回

			unset($arr[$o[2]]);
			$arr=$t.&#39;$arr=&#39;.var_export($arr,TRUE).&#39;;&#39;;
			file_put_contents($o[0],$arr,LOCK_EX);
			chmod($o[0],0777);
			unset($o,$t);
			return array(TRUE,$arr);
		case &#39;delall&#39;:	//全刪
			if(!isset($o[2])){return array(FALSE,&#39;Error:&#39;.__LINE__.&#39;,&#39;.basename(__FILE__).&#39; 參數3錯誤!&#39;);}

			$arr=$t.&#39;$arr=array();&#39;;
			file_put_contents($o[0],$arr,LOCK_EX);
			chmod($o[0],0777);
			unset($o,$t);
			return array(TRUE,array());
		}
		throw new exception(&#39;Error:&#39;.__LINE__.&#39;,&#39;.basename(__FILE__).&#39;!&#39;);die();
	default:
		unset($o,$t);
		return array(FALSE,&#39;Error:&#39;.__LINE__.&#39;,&#39;.basename(__FILE__).&#39; 參數個數錯誤!&#39;);
	}
	unset($o,$t);
	throw new exception(&#39;Error:&#39;.__LINE__.&#39;,&#39;.basename(__FILE__).&#39;!&#39;);die();
}

以上就是把內容以array的形式存入php檔案內,使用時直接require即可,該方式可達到高效緩存的目的的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

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

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

Dreamweaver CS6

Visual web development tools

DVWA

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

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment