使用 ||或在 coldfusion if 语句中使用 isDefined
Using || OR in coldfusion if statement with isDefined
我在 Coldfusion 2018 中使用会话变量,我正在尝试弄清楚如何使用我的 if 语句设置方式添加变量。
<cfif isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'andor_1') >
<cfif session.checkout.info.andor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
</cfif>
or
<cfif isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'bandor_1') >
<cfif session.checkout.info.bandor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
</cfif>
if 语句几乎相同 andor_1
或 bandor_1
但其中任何一个可能并不总是存在,这就是我使用 isDefined 的原因。
我试过使用 ||
和 or
。
<cfif isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'andor_1')
|| isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'bandor_1')>
<cfif session.checkout.info.andor_1 eq "And" || session.checkout.info.bandor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
</cfif>
任何结合这些 cfifs
的帮助将不胜感激。
CF 中的正确方法是 'OR' 而不是 ||。
但是,在您的第一个示例中,您将 "OR" 放在了 IF 语句之外。试试这个:
<cfif isDefined("session") AND structKeyExists(session, 'checkout') AND structKeyExists(session.checkout, 'info')
AND (
(structKeyExists(session.checkout.info, 'andor_1') AND session.checkout.info.andor_1 eq "And")
OR
(structKeyExists(session.checkout.info, 'bandor_1') AND session.checkout.info.bandor_1 eq "And")
)>
<strong>- All signatures are required.</strong>
</cfif>
如果您使用的是 CF2016,则可以使用安全导航运算符,或 ?.
(https://www.adobe.com/devnet/coldfusion/articles/language-enhancements-cf-2016.html)。你也应该开始使用 cfscript 来处理这些逻辑问题。
<cfscript>
// Setup my struct. Session is a struct. I renamed it for my example since it's a special word.
s = {
checkout : {
info : {
andor_1 : "And" ,
bndor_1 : "And"
}
}
} ;
//writeDump(session);
// If CF2016+ Safe Navigation Operator To The Rescue!
if( s?.checkout?.info?.andor_1 == "And" || s?.checkout?.info?.bndor_1 == "And" ) {
writeOutput("<strong>- All signatures are required.</strong>");
}
</cfscript>
https://trycf.com/gist/a82b8466c427fb40b53bbc506e4d419d/lucee5?theme=monokai
另一个选项(虽然性能不如使用 structKeyExists()
。
<cfif isDefined("session.checkout.info.andor_1") AND session.checkout.info.andor_1 eq "And"
OR isDefined("session.checkout.info.bandor_1") AND session.checkout.info.andor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
需要研究的是设置一些默认值,这样您就不需要 运行 isDefined
或 structKeyExists
检查。这有可能大大清理您的代码并使其更具可读性。
当然会有必要的例外或场景(例如,使用来自 API 的响应)。
我在 Coldfusion 2018 中使用会话变量,我正在尝试弄清楚如何使用我的 if 语句设置方式添加变量。
<cfif isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'andor_1') >
<cfif session.checkout.info.andor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
</cfif>
or
<cfif isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'bandor_1') >
<cfif session.checkout.info.bandor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
</cfif>
if 语句几乎相同 andor_1
或 bandor_1
但其中任何一个可能并不总是存在,这就是我使用 isDefined 的原因。
我试过使用 ||
和 or
。
<cfif isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'andor_1')
|| isDefined("session")
and structKeyExists(session, 'checkout')
and structKeyExists(session.checkout, 'info')
and structKeyExists(session.checkout.info, 'bandor_1')>
<cfif session.checkout.info.andor_1 eq "And" || session.checkout.info.bandor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
</cfif>
任何结合这些 cfifs
的帮助将不胜感激。
CF 中的正确方法是 'OR' 而不是 ||。
但是,在您的第一个示例中,您将 "OR" 放在了 IF 语句之外。试试这个:
<cfif isDefined("session") AND structKeyExists(session, 'checkout') AND structKeyExists(session.checkout, 'info')
AND (
(structKeyExists(session.checkout.info, 'andor_1') AND session.checkout.info.andor_1 eq "And")
OR
(structKeyExists(session.checkout.info, 'bandor_1') AND session.checkout.info.bandor_1 eq "And")
)>
<strong>- All signatures are required.</strong>
</cfif>
如果您使用的是 CF2016,则可以使用安全导航运算符,或 ?.
(https://www.adobe.com/devnet/coldfusion/articles/language-enhancements-cf-2016.html)。你也应该开始使用 cfscript 来处理这些逻辑问题。
<cfscript>
// Setup my struct. Session is a struct. I renamed it for my example since it's a special word.
s = {
checkout : {
info : {
andor_1 : "And" ,
bndor_1 : "And"
}
}
} ;
//writeDump(session);
// If CF2016+ Safe Navigation Operator To The Rescue!
if( s?.checkout?.info?.andor_1 == "And" || s?.checkout?.info?.bndor_1 == "And" ) {
writeOutput("<strong>- All signatures are required.</strong>");
}
</cfscript>
https://trycf.com/gist/a82b8466c427fb40b53bbc506e4d419d/lucee5?theme=monokai
另一个选项(虽然性能不如使用 structKeyExists()
。
<cfif isDefined("session.checkout.info.andor_1") AND session.checkout.info.andor_1 eq "And"
OR isDefined("session.checkout.info.bandor_1") AND session.checkout.info.andor_1 eq "And">
<strong>- All signatures are required.</strong>
</cfif>
需要研究的是设置一些默认值,这样您就不需要 运行 isDefined
或 structKeyExists
检查。这有可能大大清理您的代码并使其更具可读性。
当然会有必要的例外或场景(例如,使用来自 API 的响应)。