ColdFusion


CFSCRIPT ColdFusion Cheat Sheet


FOR Loop
for (i=1;i LTE ArrayLen(array);i=i+1) {
 WriteOutput(array[i]);
}
  User Defined Function (UDF)
function funky(mojo) {
 var dojo = 0;
 if (arguments.mojo EQ dojo) {
  return "mojo";
 }
 else { return "no-mojo"; }
}
Switch Case
switch(fruit) {
    case "apple":
         WriteOutput("I like Apples");
         break;
    case "orange":
         WriteOutput("I like Oranges");
         break;
    default: 
         WriteOutput("I like fruit");
}
  FOR IN Loop
struct = StructNew();
struct.one = "1";
struct.two = "2";
for (key in struct) {
 WriteOutput(key);
}
//OUTPUTS onetwo
  If / Else If / Else
if (fruit IS "apple") {
 WriteOutput("I like apples");
}
else if (fruit IS "orange") {
 WriteOutput("I like oranges");
}
else {
 WriteOutput("I like fruit");
}
  Single Line Comments
mojo = 1; //THIS IS A COMMENT
  Multi-Line Comments
/* This is a comment
 that can span
 multiple lines
*/
Try / Catch Statements
try {
    x = 8 / 0;
}
catch(Any e) {
    WriteOutput("Error: " & e.message);
}
  While Loop
x = 0;
while (x LT 5) {
 x = x + 1;
 WriteOutput(x);
}
//OUTPUTS 12345
  Do / While Loop
x = 0;
do {
 x = x+1;
 WriteOutput(x);
} while (x LTE 0);
// OUTPUTS 1
  Set A Variable
foo = "bar";
  CFSCRIPT Block
<cfscript>
if (foundeo IS "consulting") {
  //...
}
</cfscript>


Tip: Don't forget to put a semicolon; at the end of each statement.

No comments:

Post a Comment