Home  >  Article  >  Backend Development  >  How to convert variable to array type in php

How to convert variable to array type in php

青灯夜游
青灯夜游Original
2022-02-09 17:52:192124browse

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")".

How to convert variable to array type in php

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);
?>

How to convert variable to array type in php

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 = &#39;3542abc&#39;;
settype($str,"array");
echo &#39;修改后的类型为:&#39; . gettype($str) . &#39;<br>&#39;;
var_dump($str);
?>

How to convert variable to array type in php

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!

Statement:
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