我有这样的代码:
$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";
我想做的是确保$oldCount
的值存储在末尾,这样,如果files.xml中有相同数量的元素,当程序第二次运行时,它将显示-"XML中没有新元素"。
<?xml version="1.0"?>
<files>
<file>.DS_Store</file>
<file>ID2PDF_log_2.xml</file>
</files>
所以,如果我只运行这两个元素的test.php,它应该第一次显示信息。但是,当我第二次运行它时,它应该会显示消息。很明显,我在PHP的variable scope
方面很弱。我该怎么做呢?
命令行不支持没有变通的会话,因为会话通常依赖于cookie(命令行中不存在)或传递url查询参数。
相反,只需将您的号码转储到文件中,例如
file_put_contents('count.txt', $count);
然后再读入,例如
$count = file_get_contents('count.txt');