Home > Article > Backend Development > How to force convert data to string type in php
Two conversion methods: 1. Add the target type "(string)" enclosed in parentheses before the data, and the syntax "(string) specifies data". 2. Use the forced type conversion function strval() or settype(), with the syntax "strval(specified data)" or "settype(specified data 2, "string")".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php will strengthen the data Convert to string type
#Method 1: Add the target type "(string)" enclosed in parentheses before the data
(string): Convert to string
<?php header('content-type:text/html;charset=utf-8'); $n=123; var_dump($n); var_dump((string)$n); echo "<hr>"; $n=true; var_dump($n); var_dump((string)$n); echo "<hr>"; $n=null; var_dump($n); var_dump((string)$n); echo "<hr>"; ?>
##Method 2: Use cast type conversion function
<?php header("Content-type:text/html;charset=utf-8"); $n1 = 146; var_dump($n1); $str = strval($n1); echo '变量 $n1 的类型为:'.gettype($str).'<br>'; echo "<hr>"; $n2= FALSE; var_dump($n2); $str = settype($n2,"string"); echo '变量 $n2 的类型为:'.gettype($n2); ?>
Note: The value of the second parameter (set type) of the settype() function can be:
"null" (from PHP 4.2.0)
PHP Video Tutorial
"The above is the detailed content of How to force convert data to string type in php. For more information, please follow other related articles on the PHP Chinese website!