
|
|
|||
Type Juggling
PHP does not require (or support) explicit type definition in
variable declaration; a variable's type is determined by the
context in which that variable is used. That is to say, if you
assign a string value to variable An example of PHP's automatic type conversion is the addition operator '+'. If any of the operands is a float, then all operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does NOT change the types of the operands themselves; the only change is in how the operands are evaluated.
If the last two examples above seem odd, see String conversion to numbers. If you wish to force a variable to be evaluated as a certain type, see the section on Type casting. If you wish to change the type of a variable, see settype(). If you would like to test any of the examples in this section, you can use the var_dump() function.
Type CastingType casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast. The casts allowed are:
(binary) casting and b prefix forward support was added in PHP 5.2.1 Note that tabs and spaces are allowed inside the parentheses, so the following are functionally equivalent:
Casting a literal strings and variables to binary strings:
It may not be obvious exactly what will happen when casting between certain types. For more info, see these sections: |
||||
| About the NetVisits, Inc Network | Advertise Developer Tutorials hosted by HostGator. Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy. |