Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement forced type conversion in php

Shulou Source: shulou.com Published: 2022-05-31 19:14:10 10月03日 Update

This article mainly introduces the relevant knowledge of "how to achieve forced type conversion in php". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve mandatory type conversion in php" can help you solve the problem.

Forced conversion method: 1, in front of the variable to be converted with parentheses enclosed in the target type "(int)", "(bool)", "(float)", etc., will be converted to the corresponding type; 2, with the type conversion function "intval (variable)", "floatval (variable)", "strval (variable)" and so on.

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

Data type conversion refers to the conversion of variables or values from one data type to another. There are two methods of conversion, which are automatic type conversion and cast type conversion.

The following article will introduce you to the forced type conversion method of php.

There are two ways to cast and cast in PHP:

You can precede the variable to be converted with the target type enclosed in parentheses (for example, $var = (int) 3.1415;)

You can use specific type conversion functions (such as intval (), floatval (), strval (), and so on) or settype () to convert types.

1. Add the target type enclosed in parentheses before the variable to be converted

The types of variables allowed in parentheses are as follows:

(int), (integer): convert to integer

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

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

(string): convert to string type

(array): converting to an array type

(object): converts to an object type.

Example:

2. Use specific type conversion functions

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

Strval (): used to get the string value of a variable

Settype (variable, "type"), which is used to convert the variable to the type type that it refers to.

Example 1:

Example 2:

Description: the settype () function changes the original variable itself; it returns TRUE when it is set successfully and FALSE when it fails.

This is the end of the introduction to "how to cast php". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Type variable mandatory function parenthesis method data goal knowledge example usage character string Boolean article floating point industry different practical successful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Docker Xiaomi Apple