Get the App
SLTechnology News&Howtos  ›  Development  › 

How php forces a value to a numeric type

Shulou Source: shulou.com Published: 2022-06-02 07:30:37 09月27日 Update

This article focuses on "how php forces values to be numeric types". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how php forces values to numeric types".

Conversion method: 1, in front of the variable enclosed in parentheses with the target type "(int)", syntax "(int) $val"; 2, the use of intval () function, syntax "intval ($val)"; 3, the use of settype (), syntax "settype ($val," integer ")".

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

Php forces values to be numeric

Method 1: add the target type "(int)" or "(integer)" enclosed in parentheses before the variable to be converted

Method 2: use the specific conversion function intval ()

The intval () function is used to get the integer value of a variable.

The intval () function returns the integer value of the variable var by using the specified binary base conversion (the default is decimal). Intval () cannot be used for object, otherwise an E_NOTICE error will be generated and 1 will be returned.

Method 3: use the settype () function

Description:

The settype () function sets the variable $var to the specified $type type. Syntax:

Settype ($var, $type)

The value that can be set for $type:

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

The settype () function changes the type of the variable itself.

At this point, I believe you have a deeper understanding of "php how to force values to numeric 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: Types variables functions methods numbers coercion syntax content parentheses values goals usefulness learning utility deeper interest decimal practicality practicality simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Docker Linux Shulou Technology Shulou Tech Info