search
HomeBackend DevelopmentPHP TutorialIn-depth instructions for using PHP data caching_PHP tutorial
In-depth instructions for using PHP data caching_PHP tutorialJul 21, 2016 pm 03:11 PM
2httpphpcodeInstructions for usecopydatago deepofcache

Copy code The code is as follows:

// http://www.jb51.net/article/23093.htm
function set_cache($name, $value) {
    // 设置相对或者绝对目录,末尾不要加 "/"
    $cache_dir = "./cache";
    // 设置扩展名
    $cache_extension = ".php";

    $cache_str_begin = "    if (! is_array ( $value )) {
        $cache_str_middle = "\$$name = \"$value\";";
    } else {
        $cache_str_middle = "\$$name = " . arrayeval ( $value ) . ";";
    }
    $cache_str_end = "\n?>";

    $cache_str = $cache_str_begin . $cache_str_middle . $cache_str_end;

    // 缓存文件路径
    $cache_file = "$cache_dir/$name$cache_extension";
    if ($fp = @fopen ( $cache_file, "wb" )) {
        fwrite ( $fp, $cache_str );
        fclose ( $fp );
        return true;
    } else {
        echo $cache_file;
        exit ( "Can not write to cache files, please check cache directory " );
        return false;
    }
}

// 将array变成字符串, 来自discuz!
function arrayeval($array, $level = 0) {
    if (! is_array ( $array )) {
        return "\"$array\"";
    }

    $space = "";
    for($i = 0; $i         $space .= "\t";
    }
    $evaluate = "Array\n$space(\n";
    $comma = $space;
    if (is_array ( $array )) {
        foreach ( $array as $key => $val ) {
            $key = is_string ( $key ) ? "\"" . addcslashes ( $key, "\"\\" ) . "\"" : $key;
            $val = ! is_array ( $val ) && (! preg_match ( "/^\-?[1-9]\d*$/", $val ) || strlen ( $val ) > 12) ? "\"" . addcslashes ( $val, "\"\\" ) . "\"" : $val;
            if (is_array ( $val )) {
                $evaluate .= "$comma$key => " . arrayeval ( $val, $level + 1 );
            } else {
                $evaluate .= "$comma$key => $val";
            }
            $comma = ",\n$space";
        }
    }
    $evaluate .= "\n$space)";
    return $evaluate;
}

$test_array = array (
        "6b" => "a\\",
        "b",
        "c",
        array (
                "c",
                "d"
        )
);

$fileAndVarName = "newFile";

// 在生成$encode_str的时候,为使字符串中原有字符格式不变,系统在编译时会给字符串中预定义字符前加 \ 使预定义字符保留在字符串中,但输出或打印字符串的时候只会输出打印出预定义字符,不会打印出预定义字符前面的 \
$encode_str = json_encode ( $test_array );
// 因为这里要把字符串打印成PHP代码,输出的时候,字符串中预定义字符会打乱程序运行,所以要在原有转义字符前再加转移字符,使字符串输出打印时在预定义字符前转义字符也能输出
$addslashes_str = addslashes ( $encode_str ); // addslashes将字符串中预定义字符前加 \ 使其能存放在字符串中不产生作用,不参与程序运行
echo stripslashes($addslashes_str); // 反转义函数,可去掉字符串中的反斜线字符。若是连续二个反斜线,则去掉一个,留下一个。若只有一个反斜线,就直接去掉。
echo "
";


// You can pass an array object or a string converted into json, which needs to be converted into an array when used.
set_cache ( "$fileAndVarName", $addslashes_str );
var_dump ( $addslashes_str );
echo "
";
include_once "./cache/$fileAndVarName.php";
var_dump ( $$fileAndVarName );
echo "
";

$decode_arr = ( array ) json_decode ( $$fileAndVarName );
var_dump ( $decode_arr );
echo "
";

// Another method of caching, use serialize to convert the array serial number into a string, store it in a file with any extension, use fopen to open it to read the string content, and then use unserialize to deserialize it into the original data
$serialize_str = serialize ( $test_array );
echo $serialize_str; // This is the array described but here it is just a string
echo "
";
$unserialize_str = unserialize ( $serialize_str ); // Restore the described data
var_dump($unserialize_str); // Restore to $test_array, the array structure is not lost.
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326975.htmlTechArticleCopy the code as follows: ?php // http://www.jb51.net/article/23093.htm function set_cache($name, $value) { // Set a relative or absolute directory, do not add "/" at the end $cache_dir = "....
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

php怎么将url的参数转化成数组php怎么将url的参数转化成数组Apr 21, 2022 pm 08:50 PM

转化方法:1、使用“mb_substr($url,stripos($url,"?")+1)”获取url的参数部分;2、使用“parse_str("参数部分",$arr)”将参数解析到变量中,并传入指定数组中,变量名转为键名,变量值转为键值。

php怎么去掉字符串首位的tab空白符php怎么去掉字符串首位的tab空白符Apr 22, 2022 pm 07:11 PM

在php中,可以利用ltrim()函数来去掉字符串首位的tab空白符,语法为“ltrim(string)”;当只给ltrim()函数传入一个参数,用于规定要检查的字符串时,可删除该字符串开始位置的空白字符(例空格、tab制表符、换行符等)。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!