Reply to note dated 25-May-2005 05:35 :
In PHP 5.1.4, cast only affects the destination type and not the source type.
So,
<?php
$a = "abc";
$bb = (int)$a;
echo $a;
?>
gives as output:
abc
![]() | 类型戏法PHP 在变量定义中不需要(或不支持)明示的类型定义;变量类型是根据使用该变量的上下文所决定的。也就是说,如果把一个字符串值赋给变量 var,var 就成了一个字符串。如果又把一个整型值赋给 var,那它就成了一个整数。 PHP 的自动类型转换的一个例子是加号“+”。如果任何一个运算数是浮点数,则所有的运算数都被当成浮点数,结果也是浮点数。否则运算数会被解释为整数,结果也是整数。注意这并没有改变这些运算数本身的类型;改变的仅是这些运算数如何被求值。
如果上面两个例子看上去古怪的话,参见字符串转换为数值。 如果要强制将一个变量当作某种类型来求值,参见类型强制转换一节。如果要改变一个变量的类型,参见 settype()。 如果想要测试本节中任何例子的话,可以用 var_dump() 函数。
类型强制转换PHP 中的类型强制转换和 C 中的非常像:在要转换的变量之前加上用括号括起来的目标类型。 允许的强制转换有:
注意在括号内允许有空格和制表符,所以下面两个例子功能相同:
当在某些类型之间强制转换时确切地会发生什么可能不是很明显。更多信息见如下小节: ![]()
jsingh dot oberoi at gmail dot com
13-May-2006 06:23
Reply to note dated 25-May-2005 05:35 :
miracle at 1oo-percent dot de
20-Feb-2006 09:26
If you want to convert a string automatically to float or integer (e.g. "0.234" to float and "123" to int), simply add 0 to the string - PHP will do the rest.
23-Jun-2005 08:47
If you have a boolean, performing increments on it won't do anything despite it being 1. This is a case where you have to use a cast.
26-May-2005 01:35
It appears in PHP 5.0.0, that casting will convert the variable to the specified type, not return a copy of the variable converted to that type.
toma at smartsemantics dot com
10-Mar-2005 10:24
In my much of my coding I have found it necessary to type-cast between objects of different class types.
Raja
10-Feb-2005 07:05
Uneven division of an integer variable by another integer variable will result in a float by automatic conversion -- you do not have to cast the variables to floats in order to avoid integer truncation (as you would in C, for example):
memandeemail at gmail dot com
09-Dec-2004 09:29
/**
tom5025_ at hotmail dot com
25-Aug-2004 04:27
function strhex($string)
dimo dot vanchev at bianor dot com
10-Mar-2004 11:02
For some reason the code-fix posted by philip_snyder at hotmail dot com [27-Feb-2004 02:08]
philip_snyder at hotmail dot com
27-Feb-2004 11:08
Re: the typecasting between classes post below... fantastic, but slightly flawed. Any class name longer than 9 characters becomes a problem... SO here's a simple fix:
post_at_henribeige_dot_de
04-May-2003 12:37
If you want to do not only typecasting between basic data types but between classes, try this function. It converts any class into another. All variables that equal name in both classes will be copied.
yury at krasu dot ru
27-Nov-2002 05:24
incremental operator ("++") doesn't make type conversion from boolean to int, and if an variable is boolean and equals TRUE than after ++ operation it remains as TRUE, so:
29-Aug-2002 01:26
Printing or echoing a FALSE boolean value or a NULL value results in an empty string:
amittai at NOSPAMamittai dot com
21-Aug-2002 02:30
Uneven division of an integer variable by another integer variable will result in a float by automatic conversion -- you do not have to cast the variables to floats in order to avoid integer truncation (as you would in C, for example): | ![]() | |