PHP Numbers

Numbers are a data type used for mathematical calculations and numerical data processing. PHP supports different types of numbers, such as integers, floating-point numbers (floats), and provides a wide range of functions to perform mathematical operations.


Types of Numbers in PHP


Type

Description

Example

Integer

Whole numbers (positive or negative) without decimals

10, -5, 1000


Try it yourself

Float (Double)

Numbers with decimals

10.5, -3.14, 2.718


Try it yourself

Infinity

Special value for numbers too large

NaN (Not a Number)

Used for invalid calculations

Numeric Strings

Strings containing numbers that can be used in calculations

"100", "3.14"


Try it yourself


PHP Casting Strings (Type casting)


PHP offers various ways to cast a numerical value into another data type.


The(int), (integer), and intval() functions are often used to convert a value to an integer.

Example
<!DOCTYPE html>
<html>
<body>
<?php
$str = "123.45";
$intValue = (int)$str; // Converts to integer (123)
$floatValue = (float)$str; // Converts to float (123.45)
echo $intValue."<br>";
echo $floatValue;
?>
</body>
</html>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.