在PHP中使用settype而不是使用括号的typecasting,有什么区别? [英] Using settype in PHP instead of typecasting using brackets, What is the difference?

查看:192
本文介绍了在PHP中使用settype而不是使用括号的typecasting,有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,您可以将类型转换为类似这样的对象; (object)或者你可以使用settype($ var,object) - 但我的问题是两者之间有什么区别?

In PHP you can typecast something as an object like this; (object) or you can use settype($var, "object") - but my question is what is the difference between the two?

哪一个更有效/更好使用?现在我发现use(object)做的工作,但想知道为什么还有一个settype函数。

Which one is more efficient / better to use? At the moment I find using (object) does the job, but wondering why there is a settype function as well.

推荐答案

$value = "100"; //Value is a string
echo 5 + (int)$value; //Value is treated like an integer for this line
settype($value,'int'); //Value is now an integer

基本上settype是一个快捷方式:

Basically settype is a shortcut for:

$value = (type)$value;

这篇关于在PHP中使用settype而不是使用括号的typecasting,有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆