Get the App
SLTechnology News&Howtos  ›  Development  › 

How php forces a string to an array

Shulou Source: shulou.com Published: 2022-06-03 19:51:45 10月03日 Update

This article introduces the knowledge of "how php forcibly converts strings into arrays". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Php forcibly converts a string into an array: 1, add the target type "(array)" enclosed in parentheses before the string variable to be converted, syntax "(array) $str"; 2, use the settype () function to set the type of variable, syntax "settype ($str," array ")".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Method 1: add the target type enclosed in parentheses (array) before the string variable to be converted

Output result:

Method 2: use the settype () function

Settype ()-sets the type of variable

Syntax: settype ($var, $type)

The possible values for type are:

"boolean" (or "bool", starting with PHP 4.2.0)

"integer" (or "int", starting with PHP 4.2.0)

"float" (available only after PHP 4.2.0, "double" used in older versions is now disabled)

"string"

"array"

"object"

"null" (since PHP 4.2.0)

Example: convert the string $str to an array

Output result:

Note: settype () modifies the type of the variable itself, returns true if the setting is successful, and false if it fails.

So, in the above example, printing the variable $str using var_dump ($str) outputs an array structure.

That's all for "how php forcibly converts strings to arrays". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Characters strings variables arrays types output compulsion methods syntax content functions parentheses more goals knowledge results useful practical successful learned Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux Docker macOS Microsoft