dongwei1954 2019-04-05 19:07
浏览 58

如何将所有已定义的变量保存到文件中,并在新脚本中重新创建它们?

I want to define PHP variables in one script, and extract them in a sencond script later.

I think the best way to achieve this, is to use serialize and unserialize.

$all_vars = serialize(get_defined_vars());
file_put_contents('vars.txt', $all_vars);
unset($all_vars);

$all_vars = unserialize(file_get_contents('vars.txt', true));

foreach ($all_vars as $key => $value) {
   $$key = $value;
}
unset($all_vars);

$all_vars = serialize(get_defined_vars());
file_put_contents('vars2.txt', $all_vars);

I would exspect that I have 2 textfiles (vars.txt and vars2.txt) with the exact same content, but they are slightly different.

For example:

vars.txt:

"value_a";r:537;s:10:"key_a";i:1090986352011110587;s:7:"counter"

vars2.txt:

"value_a";a:1:{i:1090986352011110587;r:538;}s:10:"key_a";i:1090986352011110587;s:7:"counter"
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python时间序列如何拟合疏系数模型
    • ¥15 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
    • ¥15 MCNP里如何定义多个源?
    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 STM32驱动继电器