How would I create this statement in CF?
<cfif (not isdefined("URL.room") or #URL.room# EQ "") and (not isdefined("URL.system" or #URL.system# EQ "") and (not isdefined("URL.date") or #URL.date# EQ "")>
Obviously the parentheses don't work, but illustrate what I am trying to accomplish. What is the syntax for this?
EDIT: Ok, I understand how to use EQ and all that. I posted this in a bit of a hurry. My question is about the parentheses. Is it syntactically correct to use them this way?
16 Answers
EDIT: Ok, I understand how to use EQ and all that. I posted this in a bit of a hurry. My question is about the parentheses. Is it syntactically correct to use them this way?
Syntactically, yes. The code's syntax is correct and will not throw syntax errors.
However, it's not necessarily the best way to do it. At the very least you should have linebreaks in there, to make it more readable, like so:
<cfif (not isdefined("URL.room") or URL.room EQ "") and (not isdefined("URL.system" or URL.system EQ "") and (not isdefined("URL.date") or URL.date EQ "") >
And I would be more inclined to write it like this:
<cfif NOT ( ( isDefined('Url.Room') AND Len(Url.Room) ) OR ( isDefined('Url.System') AND Len(Url.System) ) OR ( isDefined('Url.Date') AND Len(Url.Date) ) )>
Because that's much more readable, and makes it more obvious that each row is checking the same thing.
That is assuming I was doing this in a single IF statement, anyway.
If you start getting lots of conditions to check, you might want to consider doing something like this instead:
<cfset FieldList = "Room,System,Date" /> <cfset AllFieldsValid = true /> <cfloop index="Field" list="#FieldList#"> <cfif NOT ( StructKeyExists(Url,Field) AND Len(Url[Field]) )> <cfset AllFieldsValid = false /> <cfbreak/> </cfif> </cfloop> <cfif AllFieldsValid> ...
Which might look intimidating at first, but is much easier to maintain - you just add a new item to FieldList (and you may already have a variable which serves that purporse).
Anyway, hopefully all this helps - let me know if any questions on it.
5I'd prefer...
<cfparam name="URL.room" default=""> <cfparam name="URL.system" default=""> <cfparam name="URL.date" default=""> <cfif len(URL.room) EQ 0 and len(URL.system) EQ 0 and len(URL.date) EQ 0> ... </cfif>
Or if you're comfortable with mixing non-boolean functions and boolean expression
<cfif len(URL.room) and len(URL.system) and len(URL.date)> ... </cfif>
replace the = with eq
0In CFML the comparison operators use characters rather than symbols:
== EQ != NEQ > GT >= GTE < LT <= LTE
Similarly with boolean operators:
! NOT && AND || OR
You can still use the traditional symbols in CFScript mode.
Also worth mentioning that Railo, an alternative CFML engine to Adobe ColdFusion, allows you to use the symbols in tag-based code, if there is no ambiguity with closing tag (e.g. the condition is wrapped in parentheses).
@Henry:
<cfif len(URL.room) EQ 0 and len(URL.system) EQ 0 and len(URL.date) EQ 0> ... </cfif>
Shorter:
<CFIF Len(URL.room) AND Len(URL.system) and Len(URL.date)>
Len() is better than EQ ""
You need to think your logic through a bit.
You can't check to see if room is an empty string if it is undefined.
Probably what you really need is : If (structkeyexist(URL,"room") and (Len(URL.room) eq 0 or URL.room eq 'blah')) Do something Else Do something else
I'm afraid stackoverflow cuts off your example condition on my phone, but hopefully this illustrates what you need to do.
2ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoam1qZW6Cc3vAna2appOasW6vxaKdZquklsGmucSnqw%3D%3D