Home > Article > Backend Development > How to convert variable to array type in php
Method to convert to array type: 1. Add the target type "(array)" enclosed in parentheses before the numerical variable to be converted, and the syntax is "(array) conversion variable"; 2. Use settype () function, syntax "settype(conversion variable, "array")".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php will strengthen the variable Convert to array type
Method 1: Add the target type "(array)" enclosed in parentheses before the numerical variable to be converted
(array)
: Convert to array type;
Example:
<?php var_dump((array)2); var_dump((array)0); var_dump((array)1); var_dump((array)100); ?>
Method 2: Use the settype() function
settype ($var,$type)
The function can set the variable $var
For the specified type $type
, $type
can set the value:
"boolean" (or "bool" from PHP 4.2 .0 onwards)
"integer" (or "int" onwards from PHP 4.2.0)
"float" (only Available after PHP 4.2.0, "double" used in older versions is now deprecated)
"string"
"array"
"object"
"null" (since PHP 4.2.0)
Note: The settype() function will change the type of the variable itself.
Example:
<?php header("Content-type:text/html;charset=utf-8"); $str = '3542abc'; settype($str,"array"); echo '修改后的类型为:' . gettype($str) . '<br>'; var_dump($str); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert variable to array type in php. For more information, please follow other related articles on the PHP Chinese website!