Home > Article > Backend Development > How to force conversion into string in php
How to force conversion of php into a string: First create a PHP sample file; then define a variable; and finally force conversion into a string through the string method in PHP.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
PHP forced conversion type
Get the data type:
1. If you want to check the value and type of an expression, use var_dump().
2. If you just want to get an easy-to-read type expression for debugging, use gettype().
3. To view a certain type, do not use gettype(), but use the is_type() function.
■Convert string to numeric value
Note: Don't expect to get the encoding of a character when you convert it to an integer (you probably do this in C as well). If you wish to convert between character encodings and characters, use the ord() and chr() functions.
■Forced type casting
Type casting in PHP is very similar to that in C: in the variable to be converted Preceded by the target type enclosed in parentheses.
The allowed casts are:
Note that spaces and tabs are allowed within the brackets
You can also use settype (mixed var, string type) is forced to convert.
1. Forced conversion to Boolean value (bool)|(boolean)
To explicitly convert a value to 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 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 is 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) ?>
2. Cast 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 numeric value
d. Convert from other types first Change to a bool value and then convert
Never force an unknown fraction to an integer, as this can 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
3.强制转换为浮点型 (int)|(double)|(real)|doubleval()|floatval()|intval()
精度: 0.12345678901234 // double,real都一样
数据的丢失参 字符串转换为数值
【推荐学习:《PHP视频教程》】
4.强制换为字符串 (string) |strval()
可以用 (string) 标记或者 strval() 函数将一个值转换为字符串。当某表达式需要字符串时,字符串的转换会在表达式范围内自动完成。例如当使用 echo() 或者 print() 函数时,或者将一个变量值与一个字符串进行比较的时候。
正如以上所示,将数组、对象或者资源打印出来,并不能提供任何关于这些值本身的有用的信息。请参阅函数 print_r() 和 var_dump(),对于调试来说,这些是更好的打印值的方法。
可以将 PHP 的值转换为字符串以永久地储存它们。这种方法被称为序列化,可以用函数 serialize() 来完成该操作。如果在安装 PHP 时建立了 WDDX 支持,还可以将 PHP 的值序列化为 XML 结构。
4. 强制转换为数组 (array)
<span style="font-family: NSimsun">NULL</span>
值转换成数组,将得到一个空数组。 5. 转换为对象 (object)
如果将一个对象转换成对象,它将不会有任何变化。如果其它任何类型的值被转换成对象,内置类 stdClass 的一个实例将被建立。如果该值为 NULL,则新的实例为空。数组转换成对象将使键名成为属性名并具有相对应的值。对于任何其它的值,名为 scalar 的成员变量将包含该值
6. 转换为资源 (无法转换)
由于资源类型变量保存有为打开文件、数据库连接、图形画布区域等的特殊句柄,因此无法将其它类型的值转换为资源。
■PHP 类型比较表
以下的表格显示了 PHP 类型和比较运算符在松散和严格比较时的作用。该补充材料还和类型戏法的相关章节内容有关。
The above is the detailed content of How to force conversion into string in php. For more information, please follow other related articles on the PHP Chinese website!