Php article to get to know the type of conversion

php is a weakly typed language, a data type conversion arithmetic operation can occur between different types. The parameters passed to the function or the function of the return value will be cast according to the type of constraint function.
We can also be converted by mandatory type. php between basic data types can be converted to each other is very flexible.
Try to grasp the core of the types of conversions are very important, you can reduce the bug in development, but also to enhance the development efficiency.
php basic data types have the following main types: string, integer, floating point, boolean, arrays, objects, NULL. Let based on these seven types, each of them converted.

A, is converted to a string

Integer: integer type is converted to a string. The digital 0 is converted to a string of "0."
Float: converted to floating-point type of string. If the last digit of the fractional part is zero will be ignored. The floating-point number 1.8 is converted to a character string "1.8", 0.0 float is converted to the string "0."
Boolean: true to the string "1", false empty string "."
Array: not recommended. Any strong array into a string output string "Array"
objects: magic __toString objects to be achieved by the method, the output of the __toString return value.
NULL: an empty string "."

Second, converted to a boolean:

String: string "", "0" is false, the rest is true. Here we note only "0" is false, "0.0" is true.
Integer: an integer from 0 to false, the rest is true.
Float: floating point 0.0 is false, the rest is true.
Arrays: an empty array is false, the rest is true.
Object: The object is true.
NULL: is false.

Third, converted to an integer:

String: Look at the beginning of the string can be converted to digital was digital, it can not be transferred or 0. For digital free. E E into an integer, a floating point remainder.
Float: abandon decimal places, leaving only the integer part. Not rounded.
Boolean: true is an integer 1, false is an integer from 0
arrays: an empty array to 0, and the remaining one.
Object: does not support object-integer illegal operation.
NULL: 0.

Fourth, converted to floating-point numbers:

String: Look at the beginning of the string can be converted to digital was digital, it can not be transferred or 0. For digital free. E E into an integer, a floating point remainder.
Integer: integer into a floating-point type of digital.
Boolean: true floating point number 1, false 0 to float
arrays: an empty array to 0, and the remaining one.
Object: does not support object-integer illegal operation.
NULL: 0.
 

Fifth, to an array:

String: converted to index contains an element array, the elements for the string.
Integer: converted to index containing an element array, the elements for the integer.
Float: converted to index contains an element array, the elements for the floating-point number.
Boolean: converted to index containing an element array, the elements for the Boolean value.
Object: The disclosed key property of the object is converted into an array of non-disclosure of the key attribute in the array for a particular name.
NULL: converted to an empty array.

Sixth, convert the object:

All types are converted to an object of type stdClass built.
String: stdClass into objects, object has a scalar attribute value of the character string.
Integer: stdClass into objects, object has a scalar attribute value is an integer.
Float: stdClass into objects, object has an attribute value of the scalar floating-point number.
Boolean: stdClass into an object, the object has a scalar value of the Boolean attribute.
Array: stdClass into objects, key attributes into an array of public objects.
NULL: stdClass into an object, no property.

Seven, converted to NULL

NULL conversion is not supported, you can use any variable unset function to set it to null.

Only one type NULL value is case-insensitive constant NULL.
In the following cases a variable is considered to be NULL:
is assigned the value NULL.
It has not yet been assigned.
Is unset ().

 

Reprinted from: https://www.javaidea.cn/topic/1282.html

Guess you like

Origin www.cnblogs.com/beenupper/p/12636924.html