ColdFusion 支持类型提示吗?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CF 中的变量是无类型的。尽管如果您想将变量计算为数字或字符串,您可以这样做:
并且对于字符串
Variables are typeless in CF. Although if you want to evaluate a variable as either a number or string, you could do:
and for strings
根据您希望对无法转换的数据执行什么操作,也许您需要 cfparam?
如果 form.v 不存在或不是数字,则会抛出异常。
您还可以测试一个值是否是数字(或任何其他类型):
并且您可以将字符串“转换”为数字值:
Depending on what you want to happen for data that can't be converted, maybe you want cfparam?
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):
and you can 'convert' a string a numeric value:
除了其他答案中的良好信息之外,值得一提的是,您可以使用
isvalid()
查看无类型值是否匹配一堆不同的条件:...等。还有一些更高级别的,例如:
有时您必须将无类型值强制转换为“真实”类型——例如,当您想要将参数传递给具有多个签名的 Java 方法时。您可以使用
javacast()
,如下所示: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:...etc. There are also some higher-level ones, like:
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: