如果else语句,如何更有效地组合或写入?
如果没有将整个事情写两次,是否有更好的方法来编写它?
唯一的区别是第一部分上的两个条件是检查站点地图是否存在,然后检查站点地图文件修改时间是否在过去24小时内发生了变化.如果这两个条件都为真,那么继续前进,如果这两个条件都是假的,那么移动到语句的else部分,它只是创建xml文档而不检查修改的时间是否已经改变,因为没有要检查的文件.
$time = time();
$sitemap = $_SERVER['DOCUMENT_ROOT'].'/sitemap.xml';
if (file_exists($sitemap)) { // if sitemap exists
if ($time - filemtime($sitemap) >= 1) { // 1 days
$xml = new DomDocument('1.0', 'utf-8');
$xml->formatOutput = true;
// creating base node
$urlset = $xml->createElement('urlset');
$urlset -> appendChild(
new DomAttr('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')
);
// appending it to document
$xml -> appendChild($urlset);
// building the xml document with your website content
foreach($dirlist as $file) {
if($file['type'] != 'text/x-php') continue;
//Creating single url node
$url = $xml->createElement('url');
//Filling node with entry info
$url -> appendChild( $xml->createElement('loc', 'http://www.'.$domain.$file['name']) );
$url -> appendChild( $lastmod = $xml->createElement('lastmod', date('Y-m-d', $file['lastmod'])) );
$url -> appendChild( $changefreq = $xml->createElement('changefreq', 'monthly') );
$file['name'] != '/' ? $p = '0.5' : $p = '1.0';
$url -> appendChild( $priority = $xml->createElement('priority', $p) );
// append url to urlset node
$urlset -> appendChild($url);
}
$xml->save($sitemap);
} // if time
} // if sitemap exists
else {
$xml = new DomDocument('1.0', 'utf-8');
$xml->formatOutput = true;
// creating base node
$urlset = $xml->createElement('urlset');
$urlset -> appendChild(
new DomAttr('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')
);
// appending it to document
$xml -> appendChild($urlset);
// building the xml document with your website content
foreach($dirlist as $file) {
if($file['type'] != 'text/x-php') continue;
//Creating single url node
$url = $xml->createElement('url');
//Filling node with entry info
$url -> appendChild( $xml->createElement('loc', 'http://www.'.$domain.$file['name']) );
$url -> appendChild( $lastmod = $xml->createElement('lastmod', date('Y-m-d', $file['lastmod'])) );
$url -> appendChild( $changefreq = $xml->createElement('changefreq', 'monthly') );
$file['name'] != '/' ? $p = '0.5' : $p = '1.0';
$url -> appendChild( $priority = $xml->createElement('priority', $p) );
// append url to urlset node
$urlset -> appendChild($url);
}
$xml->save($sitemap);
}
Run Code Online (Sandbox Code Playgroud)
这实际上是相同的检查而不重复代码:
if (!file_exists($sitemap) || ($time - filemtime($sitemap) >= 1)) {
// one copy of your routine
}
Run Code Online (Sandbox Code Playgroud)
这是真值表中的思考过程:
A = file_exists($sitemap)
B = ($time - filemtime($sitemap) >= 1)
A | B | Outcome
--+---+--------
1 | 1 | 1
--+---+--------
1 | 0 | 0
--+---+--------
0 | - | 1
Run Code Online (Sandbox Code Playgroud)
结合积极成果使我们:
(A && B) || !A
Run Code Online (Sandbox Code Playgroud)
切换这两个允许我们短路,使得不必A
再次检查,因此它简化为:
!A || B
Run Code Online (Sandbox Code Playgroud)
其中,用原始表达式代替,给出:
!file_exists($sitemap) || ($time - filemtime($sitemap) >= 1)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
156 次 |
最近记录: |