写if if语句的正确方法是什么? [英] Proper way to write this if else statement?

查看:134
本文介绍了写if if语句的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在if语句中如何更有效地组合或写入?

How do I combine or write more efficiently this if else statement?

有没有更好的方法来编写它而不必将整个事情写两次?

Is there a better way to write it without writing the entire thing twice?

唯一的区别是第一部分上的两个条件是检查站点地图是否存在,然后检查站点地图文件修改时间是否在过去24小时内发生了变化。如果这两个条件都为真,则向前移动,如果这两个条件为假,则转到语句的else部分,该部分只是创建xml文档而不检查修改时间是否已更改,因为没有要检查的文件。

The only difference is the two conditions placed on the first part to check if sitemap exists and then checks if the sitemap file modify time has changed in last 24 hours. If those two conditions are true then move forward, if those two conditions are false then move to the else part of the statement which simply creates the xml document without checking to see if the modified time has changed, since there was no file to check against.

$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);
}


推荐答案

这实际上是一样的检查而不复制代码:

This would be effectively the same check without duplicating the code:

if (!file_exists($sitemap) || ($time - filemtime($sitemap) >= 1)) {
    // one copy of your routine
}

这是真值表中的思考过程:

Here's the thought process in a truth table:

A = file_exists($sitemap)
B = ($time - filemtime($sitemap) >= 1)

A | B | Outcome
--+---+--------
1 | 1 | 1
--+---+--------
1 | 0 | 0
--+---+--------   
0 | - | 1

结合积极的结果给我们:

Combining the positive outcomes gives us:

(A && B) || !A

切换这两个允许我们短路,因此无需检查<$ c再次$ c> A ,因此它简化为:

Switching those two around allows us to short-circuit, making it unnecessary to check A again, so it simplifies to:

!A || B

在原始表达式中替换后,给出:

Which, substituting back in the original expressions, gives:

!file_exists($sitemap) || ($time - filemtime($sitemap) >= 1)

这篇关于写if if语句的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆