Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement forced type conversion in php

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report