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

How to convert variable to array type in php

青灯夜游
Release: 2023-03-15 07:56:02
Original
2153 people have browsed it

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);
?>
Copy after login

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);
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template