Get the App
SLTechnology News&Howtos  ›  Development  › 

How php casts strings to other types

Shulou Source: shulou.com Published: 2022-06-01 18:25:10 09月17日 Update

This article focuses on "how php converts strings to other types". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "php how to cast strings to other types"!

Cast method: 1, add the target type enclosed in parentheses before the string variable, such as "(int) $str", "(bool) $str", "(float) $str"; 2, use the conversion functions intval (), floatval (), boolval (), settype ().

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

Php casts strings to other types

Method 1: add the target type enclosed in parentheses before the string variable

(int), (integer): convert to integer

(bool), (boolean): convert to Boolean type

(float), (double), (real): convert to floating point type

(array): converting to an array type

(object): converts to an object type.

Example:

2. Use a cast function

Intval (): used to get the integer value of a variable

Floatval (): used to get the floating-point value of a variable

Boolval (): used to get the Boolean value of the variable

Settype (): used to set the variable to the specified type (the settype () function changes the original type of the variable).

Note: the value of the second parameter (the set type) of the settype () function can be:

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

At this point, I believe you have a deeper understanding of "php how to cast strings to other types". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Type variable character string force function method content Boolean parenthesis floating point goal usage learning utility deeper interest original parameter utility Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux Xiaomi NVidia vpn