写if if语句的正确方法是什么?

编程入门 行业动态 更新时间:2024-10-26 06:30:38
本文介绍了写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', 'www.sitemaps/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', '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', 'www.sitemaps/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', '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

切换这两个允许我们短路,因此无需检查 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语句的正确方法是什么?

本文发布于:2023-07-17 02:40:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128536.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   正确   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!