A brief analysis of php data Type conversion

  • 2020-12-16 05:52:47
  • OfStack

PHP does not require (or 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 becomes a string. If you assign another integer value to var, it becomes an integer.

An example of PHP's automatic type conversion is the plus sign "+". If any 1 operand is a float, all operands are treated as floats and the result is a float. Otherwise the operand will be interpreted as an integer and the result will be an integer. Note that this does not change the type of these operands themselves; All that changes is how the operands are evaluated and the type of the expression itself.

Type cast
The allowed casts are:

The & # 8226; (int), (integer) - Convert to integer (integer)
The & # 8226; (bool), (boolean) - Convert to Boolean (boolean)
The & # 8226; (float), (double), (real) - Convert to floating-point
The & # 8226; (string) - Converted to a string (string)
The & # 8226; (binary) - Converted to base 2 string (string) (PHP 6)
The & # 8226; (array) - Convert to array (array)
The & # 8226; (object) - Convert to object (object)
The & # 8226; (unset) - Convert to NULL (PHP 5)
(binary) Conversions will prefix the results with 'b' and PHP 5.2.1 will be added.

Note that Spaces and tabs are allowed in parentheses

Converts string (string) literals and variables to a 2-base string (string) :


<?php
$binary = (binary)$string;
$binary = b"binary string";
?>

To change the type of a variable, see settype();

settype - Sets the type of the variable

bool settype ( mixed $var , string $type )
Set the type of variable var to type.

The possible values of type are:

The & # 8226; "boolean" (or "bool", starting from PHP 4.2.0)
The & # 8226; "integer" (or "int", starting from PHP 4.2.0)
The & # 8226; "float" (available only after PHP 4.2.0, deactivated for "double" used in older versions)
The & # 8226; "string"
The & # 8226; "array"
The & # 8226; "object"
The & # 8226; "null" (from PHP 4.2.0 onwards)
Returns TRUE on success or FALSE on failure.

intval(), floatval(), strval(), these three functions are also convertible

The following will share the knowledge of PHP data type conversion in 1.

The data type conversion of PHP is cast. The data types of PHP that are allowed to be converted are:

(int), (integer) : convert to plastic
(float), (double), (real) : converted to floating point
(string) : Converted to a string
(bool), (boolean) : converted to a Boolean type
(array) : converted to array
(object) : Converted to objects

There are three ways to convert PHP data types:

(1) The target type enclosed in parentheses before the variable to be transformed, such as:

The following is an example to illustrate:


<?php
$num1=3.14;
$num2=(int)$num1; // Cast to int type 
var_dump($num1); // The output float(3.14)
var_dump($num2); // The output int(3)

(2) Three specific types of conversion functions are used, intval(), floatval() and strval(). Examples are as follows:


<?php
$str="123.9abc";
$int=intval($str); // Converted value: 123
$float=floatval($str); // Converted value: 123.9
$str=strval($float); // Converted string: "123.9"

(3) The general type conversion function settype(mixed var,string type) is used, with specific examples as follows:


<?php
$num4=12.8;
$flg=settype($num4,"int");
var_dump($flg); // The output bool(true)
var_dump($num4); // The output int(12)


Related articles: