How to convert php to digital format

青灯夜游
Release: 2023-03-12 16:54:01
Original
4192 people have browsed it

How to convert php to digital format: 1. Add the target type "(int)", "(float)", etc. enclosed in parentheses before the variable to be converted, and convert it to an integer or Floating point number type; 2. Use the intval() or floatval() function to convert to an integer or floating point number; 3. Use the settype() function.

How to convert php to digital format

The operating environment of this tutorial: Windows 7 system, PHP 7.1 version, DELL G3 computer

Convert php to numbers Format

1. Add the target type enclosed in parentheses before the variable to be converted and convert it to an integer or floating point number type

  • (int), (integer): Convert to integer type;

  • (float ), (double), (real): Convert to floating point type;

Example:

<?php
$str = &#39;123.456abc&#39;;
var_dump($str);

$int1 = (int)$str;
var_dump($int1);
$int2 = (integer)$str;
var_dump($int2);

$float1 = (float)$str;
var_dump($float1);

$float2 = (double)$str;
var_dump($float2);

$float3 = (real)$str;
var_dump($float3);
?>
Copy after login

Output result:

How to convert php to digital format

Method 2: Use conversion function intval(), floatval()

  • intval(): used to get the integer value of the variable;

  • floatval(): used to get the floating point value of the variable;

Example:

<?php
$str = &#39;123.456abc&#39;;
var_dump($str);

$int = intval($str);
var_dump($int);

$float = floatval($str);
var_dump($float);
Copy after login

Output result:

How to convert php to digital format

##Method 3: Use settype() function

The syntax format of this function is as follows:


settype(mixed &$var, string $type)
Copy after login

Among them, $var is the variable to be converted; $type is the type to be converted, which can be boolean (bool), integer (int), float (double), string, array, object, null.

Note: The settype() function will change the type of the variable itself

Example:

<?php
header("Content-type:text/html;charset=utf-8"); 
$str = &#39;123.456abc&#39;;
var_dump($str);

settype($str, &#39;integer&#39;);
var_dump($str);

$str = &#39;123.456abc&#39;;
settype($str, &#39;float&#39;);
var_dump($str);
Copy after login
Output result:

How to convert php to digital format

Recommended learning:

php training

The above is the detailed content of How to convert php to digital format. 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