coldfusion 指数
示例
参数
基本索引循环
最终值为x10。
<!--- Tags --->
<cfoutput>
<cfloop index="x" from="1" to="10">
<li>#x#</li>
</cfloop>
</cfoutput>
<!--- cfscript --->
<cfscript>
for (x = 1; x <= 10; x++) {
writeOutput('<li>' & x & '</li>');
}
</cfscript>
<!--- HTML Output --->
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10增加到2
最终值为x11。
<!--- Tags --->
<cfoutput>
<cfloop index="x" from="1" to="10" step="2">
<li>#x#</li>
</cfloop>
</cfoutput>
<!--- cfscript --->
<cfscript>
for (x = 1; x <= 10; x += 2) {
writeOutput('<li>' & x & '</li>');
}
</cfscript>
<!--- HTML Output --->
- 1
- 3
- 5
- 7
- 9递减1
最终值为x0。
<!--- Tags --->
<cfoutput>
<cfloop index="x" from="10" to="1" step="-1">
<li>#x#</li>
</cfloop>
</cfoutput>
<!--- cfscript --->
<cfscript>
for (x = 10; x > 0; x--) {
writeOutput('<li>' & x & '</li>');
}
</cfscript>
<!--- HTML Output --->
- 10
- 9
- 8
- 7
- 6
- 5
- 4
- 3
- 2
- 1功能中的CFLoop
确保在函数内部索引var或确定local索引范围。Foo()返回11。
<!--- var scope --->
<cffunction name="foo" access="public" output="false" returntype="numeric">
<cfset var x = 0 />
<cfloop index="x" from="1" to="10" step="1">
<cfset x++ />
</cfloop>
<cfreturn x />
</cffunction>
<!--- Local scope --->
<cffunction name="foo" access="public" output="false" returntype="numeric">
<cfloop index="local.x" from="1" to="10" step="1">
<cfset local.x++ />
</cfloop>
<cfreturnlocal.x/>
</cffunction>通过电流的ColdFusion11
cfscript函数cfloop不支持index作为独立的计数器机制。