How to convert string to number in php

Guanhui
Release: 2023-02-28 21:08:01
Original
3387 people have browsed it

How to convert string to number in php

php method to convert string to number

intval() function definition and usage

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

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

Syntax

intval ( string [, base = 10 ] )
Copy after login

Parameters

string: The quantity value to be converted to integer.

base: The base used for conversion.

If base is 0, determine the base used by detecting the format of var:

If the string includes the prefix of "0x" (or "0X"), use hexadecimal (hex); otherwise,

if the string starts with "0", use octal; otherwise,

will use decimal.

Return value

Returns the integer value of var on success, and returns 0 on failure. An empty array returns 0, a non-empty array returns 1.

The maximum value depends on the operating system. The maximum signed integer range on 32-bit systems is -2147483648 to 2147483647. For example, on such a system, intval('1000000000000') would return 2147483647. On a 64-bit system, the maximum signed integer value is 9223372036854775807.

String may return 0, although it depends on the leftmost character of the string.

intval uses

<?php
echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval(&#39;42&#39;);                    // 42
echo intval(&#39;+42&#39;);                   // 42
echo intval(&#39;-42&#39;);                   // -42
echo intval(042);                     // 34
echo intval(&#39;042&#39;);                   // 42
echo intval(1e10);                    // 1410065408
echo intval(&#39;1e10&#39;);                  // 1
echo intval(0x1A);                    // 26
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);   // 0
echo intval(&#39;420000000000000000000&#39;); // 2147483647
echo intval(42, 8);                   // 42
echo intval(&#39;42&#39;, 8);                 // 34
echo intval(array());                 // 0
echo intval(array(&#39;foo&#39;, &#39;bar&#39;));     // 1
Copy after login

The above is the detailed content of How to convert string to number in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!