在每次执行script-PHP时存储变量值
我有以下代码:在每次执行script-PHP时存储变量值,php,xml,Php,Xml,我有以下代码: $filename = 'files.xml'; $dom = new DomDocument(); $dom->load($filename); $oldCount = ''; $newCount = $dom->getElementsByTagName('file')->length; if($newCount == $oldCount){ echo "There are no new elements in the XML.\n"; } else
$filename = 'files.xml';
$dom = new DomDocument();
$dom->load($filename);
$oldCount = '';
$newCount = $dom->getElementsByTagName('file')->length;
if($newCount == $oldCount){
echo "There are no new elements in the XML.\n";
}
else {
echo "New Count is: ".$newCount."\n";
echo "Old Count is: ".$oldCount."\n";
for ($oldCount =0; $oldCount < $newCount; $oldCount++){
$file = file_get_contents('files.xml');
$xml = simplexml_load_string($file);
$result = $xml->xpath('file');
echo "File ".($oldCount+1).": ".$result[$oldCount]."\n\n";
//echo "Old: ".$oldCount."\n";
//echo "New: ".$newCount."\n";
}
$oldCount = $newCount;
//echo "Old Count at the end: ".$oldCount."\n";
}
echo "Old Count at the end: ".$oldCount."\n";
因此,如果我只使用这两个元素运行test.php,它应该第一次显示信息。但是,当我第二次运行它时,它应该会显示消息。
很明显,我在PHP中的变量范围方面很弱。
我该怎么做 命令行不支持没有解决方法的会话,因为会话通常依赖于cookie(命令行中不存在cookie)或传递url查询参数
相反,只需将您的号码转储到一个文件中,例如
file_put_contents('count.txt', $count);
然后稍后再阅读
$count = file_get_contents('count.txt');
您是在浏览器(如apache)中运行tour php脚本,还是作为cli脚本运行?您应该在会话或数据库中保存以前的值。这不是在浏览器上运行的。我在客户端和两台服务器上完成这一切。我不能将它存储在数据库中,因为我没有这个选项。如果我不是真的使用浏览器,而是从命令行运行脚本,我还可以使用会话吗?我根本没有想到这一点(谢谢,@Marc。我会试试这个。我想应该有用。:)小更正!!它是文件内容('count.txt',$count)代码>在我的情况下!很好用!再次感谢!:)
$count = file_get_contents('count.txt');