How to convert int in php

藏色散人
Release: 2023-03-17 10:24:02
Original
2530 people have browsed it

How to convert int in php: 1. Create a PHP sample file; 2. Determine the value to be converted; 3. Use the "(int)" or "(integer)" method to force conversion, the syntax is as follows "var_dump((int) "221");echo "
";var_dump((integer)"-25"".

How to convert int in php

This tutorial operation Environment: Windows 7 system, PHP version 8.1, Dell G3 computer.

php How to convert int?

php int type conversion

int is a collection ℤ = a number in {…, -2, -1, 0, 1, 2, …}.

To convert a value to int,

Use (int) or (integer) cast.

However, in most cases no cast is required, because when an operator, function or flow control requires an int parameter, the value is automatically converted. You can also use the function intval() To convert a value to int type.

intval() syntax

intval(mixed $value, int $base = 10): int
Copy after login

Parameters

value

The quantity value to be converted to integer

base

The hexadecimal system used for conversion

<?php
var_dump(
    (int) "221"                              // int(221)
);
echo "<br>";
var_dump(
    (integer)"-25"                          //int(-25)
);
echo "<br>";
echo intval(42);                      // 42
echo "<br>";
echo intval(4.2);                     // 4
echo "<br>";
echo intval(&#39;42&#39;);                    // 42
echo "<br>";
echo intval(&#39;+42&#39;);                   // 42
echo "<br>";
echo intval(&#39;-42&#39;);                   // -42
echo "<br>";
echo intval(042);                     // 34
echo "<br>";
echo intval(&#39;042&#39;);                   // 42
echo "<br>";
echo intval(2e2);                    // 200 e后面表示添加几个0
echo "<br>";
echo intval(&#39;1e10&#39;);                  // 1
echo "<br>";
echo intval(0x1A);                    // 26 0x开头是16进制
echo "<br>";
echo intval(42000000);                // 42000000
echo "<br>";
echo intval(420000000000000000000);   // -4275113695319687168
echo "<br>";
echo intval(&#39;420000000000000000000&#39;); // 9223372036854775807
echo "<br>";
echo intval(42, 8);                   // 42
echo "<br>";
echo intval(&#39;42&#39;, 8);                 // 34
echo "<br>";
echo intval(array());                 // 0
echo "<br>";
echo intval(array(&#39;foo&#39;, &#39;bar&#39;));     // 1
echo "<br>";
echo intval(false);                   // 0
echo intval(true);                    // 1
?>
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to convert int 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