Data type Casting in PHP _php tutorial

Source: Internet
Author: User
Tags type casting
PHP is a loose type of language, do not have to use the variable to do a special definition, which gives the program to write a lot of flexibility and convenience. But in the process of programming, we need to know what kind of variables we use, because variables always have one type corresponding to them. Although there can be almost free conversions between types, if you use or convert variable types randomly, some potential errors can result.

The type casts in PHP are very similar to those in C: precede the variables to be converted with the target type enclosed in parentheses:

The code is as follows Copy Code

$foo = 10; $foo for integer type
$bar = (Boolean) $foo; $bar is a Boolean type
?>


Type conversions
PHP does not need (or does not support) explicit type definitions in variable definitions; The variable type is determined by the context in which the variable is used. That is, if you assign a string value to the variable Var,var it becomes a string. If you assign an integer value to Var, it becomes an integer.

An example of automatic type conversion for PHP is the plus sign "+". If any one operand is a floating-point number, all operands are treated as floating-point numbers, and the result is a floating-point number. Otherwise the operand is interpreted as an integer, and the result is an integer. Note that this does not change the type of the operands themselves; it only changes how the operands are evaluated and the type of the expression itself.

Type casting
The type casts in PHP are very similar to those in C: precede the variables to be converted with the target type enclosed in parentheses:

The code is as follows Copy Code
$foo = 10; $foo for integer type
$bar = (Boolean) $foo; $bar is a Boolean type
?>

The allowable casts are:

(int) or (integer)-Convert to Integer type
(bool) or (Boolean)-Converts to Boolean
(float) or (double) or (real)-converts to floating-point type
(string)-converts to a string
(array)-Convert an array
(object)-Convert to Object
In addition, a variable is reverted to a string, and the variable can be placed in double quotation marks:

Convert numbers to character turns

The code is as follows Copy Code

$foo = 10; $foo for integer type
$str = "$foo"; $str as a string
?>


There's a super-easy way to convert the strings we use to integers.

The code is as follows Copy Code

$str =www.bkjia.c0m;
$int = Intval ($STR);
It's so $int=0, OH.

When a string is evaluated as a number, the type and value of the result is determined according to the following rules:

If any one of the characters is included in ".", "E" or "E", the string is evaluated as a float, otherwise it is treated as an integer
The value is determined by the first part of the string. If the string starts with a valid numeric data, the number is used as its value, otherwise its value is 0 (0). Valid numeric data starts with an optional sign, followed by one or more digits (optionally including a decimal fraction) followed by an optional exponent. Index is an "E" or "E" followed by one or more numbers
Example:

The code is as follows Copy Code

$foo = 1 + "10.5"; $foo is floating point type: 11.5
$foo = 1 + " -1.3e3"; $foo is floating point type:-1299
$foo = 1 + "bob-1.3e3"; $foo to Integer type: 1
$foo = 1 + "BOB3"; $foo to Integer type: 1
$foo = 1 + "Ten Small Pigs"; $foo to Integer type: 11
$foo = "10.0 pigs" + 1; $foo is floating point type: 11

?>

Since PHP does not need to define variables at the time of data usage, we can flexibly define variables and also flexibly convert data types.

http://www.bkjia.com/PHPjc/629165.html www.bkjia.com true http://www.bkjia.com/PHPjc/629165.html techarticle php is a loose type of language, do not have to use the variable to do a special definition, which gives the program to write a lot of flexibility and convenience. But in the process of programming, we need to know ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.