ColdFusion 支持类型提示吗?

发布于 2024-10-07 23:05:53 字数 90 浏览 0 评论 0原文

在 PHP 中我可以这样做: $var = (int) $_POST['var'];

有没有办法将 ColdFusion 变量显式设置为整数或字符串?

In PHP I can do:
$var = (int) $_POST['var'];

Is there a way to explicitly set a ColdFusion variable to an integer or a string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你在我安 2024-10-14 23:05:53

CF 中的变量是无类型的。尽管如果您想将变量计算为数字或字符串,您可以这样做:

#Val(<variable>)# 

并且对于字符串

#ToString(<variable>)#

Variables are typeless in CF. Although if you want to evaluate a variable as either a number or string, you could do:

#Val(<variable>)# 

and for strings

#ToString(<variable>)#
李不 2024-10-14 23:05:53

根据您希望对无法转换的数据执行什么操作,也许您需要 cfparam?

<cfparam name="form.v" type="numeric"/>

如果 form.v 不存在或不是数字,则会抛出异常。

您还可以测试一个值是否是数字(或任何其他类型):

<cfif isNumeric(form.v)>

并且您可以将字符串“转换”为数字值:

<cfset v = val(form.v) />

Depending on what you want to happen for data that can't be converted, maybe you want cfparam?

<cfparam name="form.v" type="numeric"/>

That will throw an exception if form.v is not present or is not numeric.

You can also test whether a value is numeric (or any other type):

<cfif isNumeric(form.v)>

and you can 'convert' a string a numeric value:

<cfset v = val(form.v) />
橘虞初梦 2024-10-14 23:05:53

除了其他答案中的良好信息之外,值得一提的是,您可以使用 isvalid() 查看无类型值是否匹配一堆不同的条件:

isvalid('integer',x);
isvalid('float',x);
isvalid('string',x);

...等。还有一些更高级别的,例如:

isvalid('email',x);
isvalid('telephone',x);

有时您必须将无类型值强制转换为“真实”类型——例如,当您想要将参数传递给具有多个签名的 Java 方法时。您可以使用 javacast(),如下所示:

x = "01";
myJavaFunc.doSomething( x ); // ambiguous -- could be a string or number
myJavaFunc.doSomething( javacast('int', x ) ); // does something
myJavaFunc.doSomething( javacast('string', x ) ); // does something else

In addition to the good information in the other answers, it's worth mentioning that you can use isvalid() to see if a typeless value matches a bunch of different criteria:

isvalid('integer',x);
isvalid('float',x);
isvalid('string',x);

...etc. There are also some higher-level ones, like:

isvalid('email',x);
isvalid('telephone',x);

There are times when you must coerce a typeless value into a "true" type -- for instance, when you want to pass an argument to a Java method with more than one signature. You'd use javacast(), like so:

x = "01";
myJavaFunc.doSomething( x ); // ambiguous -- could be a string or number
myJavaFunc.doSomething( javacast('int', x ) ); // does something
myJavaFunc.doSomething( javacast('string', x ) ); // does something else
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文