Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What are the ways for php to convert string type into int type?
What are the ways for php to convert string type into int type?
Php can convert string type to int type: 1, forcing type conversion method; 2. Built-in function method; 3. Format string method. The method of forced type conversion refers to adding the target type enclosed in brackets before the variable to be converted, such as (int)$var.

Specific methods:

(Video tutorial recommendation: php video tutorial)

1, forcing type conversion mode

Forced type conversion is a way of "putting the target type in parentheses before the variable to be converted" (from the "Type Skills" section of the PHP manual).

& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)

$ foo = " 1 "; // $foo is a string type.

$ bar =(int)$ foo; // $bar is an integer

& gt for integers, the conversion type name is int or integer.

2. Built-in function mode

The way to build a function is to use PHP's built-in function intval to convert variables.

& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)

$ foo = " 1 "; // $foo is a string type.

$ bar = intval($ foo); // $bar is an integer

& gt3. Format string mode

The way to format the string is to format the specified variable with sprintf's %d to achieve the purpose of type conversion.

& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)

$ foo = " 1 "; // $foo is a string type.

$bar = sprintf("%d ",$ foo); // $bar is a string type

& gt Strictly speaking, the conversion result of sprintf is still a string type, so it should not be regarded as a way to convert a string into an integer. But the string value he processed has indeed become "an integer that is forcibly converted into a string type".