Detailed explanation of PHP intval() function usage

WBOY
Release: 2023-06-27 18:58:02
Original
2338 people have browsed it

PHP is a very popular programming language with many built-in functions to handle numeric values. Among them, the intval() function is a commonly used function used to convert a value to an integer type. This article will introduce the usage of PHP intval() function in detail.

1. Basic use of intval() function

The basic syntax of intval() function is as follows:

intval ( mixed $var [, int $base = 10 ] ) : int
Copy after login

Among them, $var represents the value to be converted, $base indicates the base to use (default is 10).

Below we use some examples to demonstrate the basic usage of the intval() function.

1. Convert floating point number to integer:

$float_num = 3.14;
$integer_num = intval($float_num);  //$integer_num输出3
Copy after login

2. Convert string to integer:

$string_num = "123";
$integer_num = intval($string_num);  //$integer_num输出123
Copy after login

3. Convert string with leading 0 to Integer (the $base parameter needs to be set at this time):

$octal_num = "0123";
$integer_num = intval($octal_num, 8);  //$integer_num输出83,表示8进制数0123转换为十进制数83
Copy after login

2. Notes on the intval() function

1. The intval() function converts a non-numeric value into an integer , will return 0. For example:

$non_number = "hello";
$integer_num = intval($non_number);  //$integer_num输出0
Copy after login

In this case, we can modify this behavior by setting the value of the second parameter $base, for example:

$non_number = "hello";
$integer_num = intval($non_number, 16);  //$integer_num输出0,表示将"hello"视为16进制数,转换为0
Copy after login

2. When converting a floating point number When it is an integer, the intval() function will round it down. For example:

$float_num = 3.8;
$integer_num = intval($float_num);  //$integer_num输出3
Copy after login

If you need to round it to an integer, you can use the round() function. For example:

$float_num = 3.8;
$integer_num = round($float_num);  //$integer_num输出4
Copy after login

3. When converting a string with leading 0s into an integer, you need to set the value of the second parameter $base. Otherwise, PHP might treat it as an octal number. For example:

$octal_num = "0123";
$integer_num = intval($octal_num);  //$integer_num输出83,表示将"0123"视为8进制数,转换为十进制数83
Copy after login

3. Summary

This article introduces the basic usage and precautions of the intval() function in PHP. The intval() function is a very commonly used conversion function that can convert various types of values ​​into integer types. When using the intval() function, you need to pay special attention to the value of the parameter $base to avoid erroneous results.

The above is the detailed content of Detailed explanation of PHP intval() function usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template