Home > Backend Development > PHP Problem > What types of casts are there in php?

What types of casts are there in php?

coldplay.xixi
Release: 2023-03-02 20:58:01
Original
3059 people have browsed it

php forced conversion types include: 1. Convert to integer type; 2. Convert to Boolean type; 3. Convert to floating point type; 4. Convert to string; 5. Convert to array; 6. Convert into object.

What types of casts are there in php?

php coercion types are:

Type coercion in PHP is very similar to that in C: in The variable to be converted is preceded by the target type enclosed in parentheses.

The allowed casts are:

  • (int),(integer) - Convert to integer type

  • (bool ),(boolean) - Convert to Boolean type

  • (float),(double),(real) - Convert to floating point type

  • (string)                                                                                                                                                                                                                                                  through

  • Note that spaces and tabs are allowed within the brackets

    You can also use settype (mixed var, string type) for forced conversion.
  • 1. Force conversion to Boolean value (bool)|(boolean)

To explicitly convert a value into boolean, use (bool) or (boolean) to force conversion. But in many cases, casting is not necessary because when an operator, function, or flow control requires a boolean parameter, the value is automatically converted.

When converted to boolean, the following values ​​are considered FALSE:

Boolean value FALSE

Integer value 0 (zero)

Floating point type Value 0.0 (zero)

Blank string and string "0"

Array with no member variables

Object without cells (only for PHP 4)

Special type NULL (including variables that have not been set)

All other values ​​are considered TRUE (including any resources).

<?php
var_dump((bool) ""); // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
?>
Copy after login

2. Force conversion to integer (int)|(integer)

To explicitly convert a value to integer, use (int) or (integer) Cast. In most cases, however, casting is not necessary because when an operator, function, or flow control requires an integer parameter, the value is automatically converted. You can also use the function intval() to convert a value to an integer type.

a. Convert from bool

b. Convert from floating point number, rounding, out of range, the result is uncertain

c. Convert from string, see Convert string to numerical value

d. Convert from other types first to bool value, and then convert

Never force an unknown fraction to an integer, as this may sometimes lead to unexpected results.

<?php
echo (int) ( (0.1+0.7) * 10 ); // 显示 7
?>
$str = "123.456abc7"; // (int)123
echo (int)$str;
$str = "abc123.456";   // (int)0
$str = true;           // (int)1
$str = false;          // (int)0
Copy after login

3. Force conversion to floating point type (int)|(double)|(real)|doubleval()|floatval()|intval()

Precision: 0.12345678901234 // Both double and real are the same

The missing parameter string of the data is converted into a numerical value

4. Forced conversion to string (string) |strval()

You can use the (string) notation or the

strval()

function to convert a value to a string. When an expression requires a string, the conversion of the string is done automatically within the scope of the expression. For example, when using the

echo()

or

print() function, or when comparing a variable value to a string.

The Boolean value TRUE will be converted to the string "1", while the value FALSE will be represented as "" (i.e. the empty string). This allows you to compare between booleans and strings at will. When integer or floating-point values ​​are converted into strings, the string consists of numeric characters representing these values ​​(floating-point numbers also contain an exponent part).

  • The array will be converted into the string "Array", so the contents of the array cannot be output through the echo() or print() functions. See below for more tips.

  • The object will be converted to the string "Object". If you need to print out the member variables of the object for debugging purposes, please read below. If you wish to get the name of the class to which the object is attached, use the function get_class(). Since PHP 5, you can use the __toString() method if appropriate.

  • Resource types are always converted to strings in the format "Resource id #1", where 1 is the unique identifier assigned to the resource by PHP at runtime. If you wish to get the type of a resource, use the function get_resource_type().

  • NULL will be converted to the empty string.

  • As shown above, printing an array, object, or resource does not provide any useful information about the values ​​themselves. See functions

    print_r()

    and
  • var_dump()
  • , which are better ways of printing values ​​for debugging.

    You can convert PHP values ​​to strings to store them permanently. This method is called serialization and can be accomplished using the function serialize(). If you set up WDDX support when you installed PHP, you can also serialize PHP values ​​into XML structures.

    5. Cast to array(array)

    For any type: integer, floating point, string, Boolean and resource, if you convert a value For an array, you will get an array with only one element (its subscript is 0), which is the value of this scalar.

    If an object is converted into an array, the elements of the resulting array are attributes (member variables) of the object, and their key names are member variable names.

    If you convert a NULL value to an array, you will get an empty array.

    6. Convert to object (object)

    If you convert an object into an object, it will not change in any way.

    If any other type of value is converted to an object, an instance of the built-in class stdClass will be created.

    If the value is NULL, the new instance is empty. Converting an array to an object will cause the keys to become property names with corresponding values.

    For any other value, the member variable named scalar will contain the value

    7. Convert to resource (cannot be converted)

    Due to Resource type variables hold special handles for open files, database connections, graphics canvas areas, etc., so other types of values ​​cannot be converted into resources.

    Note

    • HTML forms do not pass integers, floating point numbers or Boolean values, they only pass strings. To check whether a string is a number, you can use the is_numeric() function.

    • When the variable $x is not defined, usage such as if ($x) will cause an E_NOTICE level error. Therefore, you can consider using empty

    Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of What types of casts are there in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template