Mandatory Data type conversion in php _ PHP Tutorial

Source: Internet
Author: User
Forced conversion of data types in php. PHP is a loose language and does not need to define the variables used. This makes programming flexible and convenient. However, in the programming process, we need to know that PHP is a loose language and do not need to define the variables used, this makes programming highly flexible and convenient. However, during programming, we need to know which type of variables we use, because there is always a corresponding type of variables. Although almost all types can be converted freely, using or converting variable types at will may lead to some potential errors.

The forced type conversion in PHP is very similar to that in C: add the target type enclosed in parentheses before the variable to be converted:

The code is as follows:

$ Foo = 10; // $ foo is an integer.
$ Bar = (boolean) $ foo; // $ bar is boolean
?>


Type conversion
PHP does not need (or does not support) explicit type definitions in variable definitions. variable types are determined by the context in which the variable is used. That is to say, if a string value is assigned to the variable var, var becomes a string. If another integer value is assigned to var, it becomes an integer.

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

Type forced conversion
The forced type conversion in PHP is very similar to that in C: add the target type enclosed in parentheses before the variable to be converted:

The code is as follows:
$ Foo = 10; // $ foo is an integer.
$ Bar = (boolean) $ foo; // $ bar is boolean
?>

The following mandatory conversions are allowed:

(Int) or (integer)-convert to integer
(Bool) or (boolean)-convert to boolean
(Float) or (double) or (real)-convert to floating point type
(String)-convert to string
(Array)-convert to an array
(Object)-convert to object
In addition, you can restore a variable to a string and place the variable in double quotation marks:

Convert numeric into character

The code is as follows:

$ Foo = 10; // $ foo is an integer.
$ Str = "$ foo"; // $ str is a string
?>


There is an extremely simple method to convert a string into an integer.

The code is as follows:

$ Str = www. bKjia. c0m;
$ Int = intval ($ str );
So $ int = 0.

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

If the string contains any of the characters ".", "e", or "E", it is evaluated as a float. Otherwise, it is treated as an integer.
This value is determined by the first part of the string. If a string starts with a valid number, use this number as its value. Otherwise, its value is 0 (0 ). Valid numeric data starts with an optional plus or minus sign, followed by one or more numbers (including decimal scores), followed by an optional index. An index is an "e" or "E" followed by one or more numbers.
Example:

The code is as follows:

$ Foo = 1 + "10.5"; // $ foo is float: 11.5
$ Foo = 1 + "-1.3e3"; // $ foo:-1299
$ Foo = 1 + "bob-1.3e3"; // $ foo is an integer: 1
$ Foo = 1 + "bob3"; // $ foo is an integer: 1
$ Foo = 1 + "10 Small Pigs"; // $ foo is an integer: 11
$ Foo = "10.0 pigs" + 1; // $ foo is a floating point: 11

?>

Because php does not need to define variables when using data, we can flexibly define variables and convert data types.

Bytes. But we need to know...

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.