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


  1. Integers
  2. Floats (Floating-Point Numbers or Doubles)
  3. Number Strings


PHP has two more data types


  1. Infinity
  2. NaN


PHP Integers


  • Integers are whole numbers, without a decimal point.
  • They can be positive, negative, or zero.



        Range of an Integer:


               On a 32-bit system, integers typically range from -2,147,483,648 to 2,147,483,647.


               On a 64-bit system, the range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.


        Integer values can be represented in different bases:


                Decimal (Base 10): 123


                Hexadecimal (Base 16): Prefixed with 0x (e.g., 0x1A)


                Octal (Base 8): Prefixed with 0 (e.g., 0123)


                Binary (Base 2): Prefixed with 0b (e.g., 0b1010)


        Predefined Constants for integers


  • PHP_INT_MAX - The largest integer supported
  • PHP_INT_MIN - The smallest integer supported
  • PHP_INT_SIZE - The size of an integer in bytes


is_int() , is_integer() , is_long() functions to check if the type of a variable is integer.



Example
<!DOCTYPE html>
<html>
<body>

<?php

$value = 10;
echo is_int($value) ? "Yes<br>" : "No<br>"; // Outputs: Yes
$x = 4356;
var_dump(is_int($x));

echo "<br>";

$x = 43.56;
var_dump(is_long($x));
?>

</body>
</html>

Try it yourself

PHP Floats (Floating-Point Numbers)


  • Floats are numbers with a decimal point or in exponential form (e.g., 1.5e3).
  • Useful for representing fractional values or numbers with a precision.


        Predefined Constants for integers


  • PHP_FLOAT_MAX - The largest representable floating point number
  • PHP_FLOAT_MIN - The smallest representable positive floating point number
  • PHP_FLOAT_DIG - The number of decimal digits that can be rounded into a float and back without precision loss
  • PHP_FLOAT_EPSILON - The smallest representable positive number x, so that x + 1.0 != 1.0


is_float()is_double() functions to check if the type of a variable is float.


Example
<!DOCTYPE html>
<html>
<body>

<?php

$x = 4356;
var_dump(is_float($x));

echo "<br>";

$x = 43.56;
var_dump(is_double($x));
?>

</body>
</html>

Try it yourself

PHP Numerical Strings


Numerical strings are strings that contain numbers and can be treated as numbers in certain situations. When a string contains a numeric value (either integer or floating-point), PHP can automatically treat it as a number when used in arithmetic operations or comparisons.

Example
<!DOCTYPE html>
<html>
<body>

<?php

$numString = "42";
$sum = $numString + 8; // Converts "42" to an integer and performs addition
echo $sum; // Outputs: 50
var_dump(is_numeric($numString));

$x = "Hello";
var_dump(is_numeric($x));


?>

</body>
</html>

Try it yourself

PHP Infinity


Infinity is a special value that represents a number beyond the range that can be represented by a float. It is denoted by the constant INF for positive infinity and -INF for negative infinity.


How to Work with Infinity in PHP


  1. Generating Infinity:
  • You can generate positive infinity using division by zero for floats or by using a number larger than PHP_FLOAT_MAX.
  • Negative infinity is produced by dividing a negative number by zero or using a number smaller than -PHP_FLOAT_MAX.


is_finite()is_infinite() functions to check if a numeric value is finite or infinite.

Example
<!DOCTYPE html>
<html>
<body>

<?php

// Positive infinity
$positiveInfinity = 1.0 / 0; // Outputs: INF
$positiveInfinityAlt = PHP_FLOAT_MAX * 2; // Outputs: INF

// Negative infinity
$negativeInfinity = -1.0 / 0; // Outputs: -INF
$negativeInfinityAlt = -PHP_FLOAT_MAX * 2; // Outputs: -INF

echo $positiveInfinity; // Displays: INF
echo $negativeInfinity; // Displays: -INF

?>

</body>
</html>

Try it yourself

PHP NaN


NAN (Not A Number) is a special floating-point value that represents the result of an undefined or unrepresentable mathematical operation.


For example, operations like taking the square root of a negative number or dividing zero by zero can produce a NaN result.


To check if a value is NaN, you can use the is_nan() function. This function returns true if the value is NAN, otherwise false.

Example
<!DOCTYPE html>
<html>
<body>

<?php
// Invalid calculation will return a NaN value
$x = sqrt(-8);

var_dump($x);
?>

</body>
</html>

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.