Numbers In PHP

Dive into the world of PHP numbers with this comprehensive guide. We will explore the various techniques and methods for handling numbers in PHP, complete with practical examples to help you understand the concepts better.

The world of programming revolves around numbers, and PHP is no exception. The PHP server side scripting language uses numbers for a variety of tasks such as mathematical computations, manipulation of data, and decision making.

PHP Numbers

We are going to look in depth at Integers, Floats, and Number Strings.



PHP Numbers

You can easily convert data types using PHP Numbers because it provides automatic data type conversions.

An integer value will automatically translate into an integer type if it is assigned to a variable.

If you then assign a string to the same variable, the type of the variable will change to a string.

In some cases, this automatic conversion can cause your code to break.


Integers In PHP

The following numbers are all integers:

2, 256, –256, 10358, -179567.

In PHP, an integer is a number that does not have a decimal part.

When we talk about Php Numbers, There are two types of integer data types: one is a non-decimal number between -2147483648 and 2147483647 in 32 bit systems, and the other is a non-decimal number between -9223372036854775808 and 9223372036854775807 in 64 bit systems.

It is likely that any value above (or below) this will be stored as a float, for the simple reason that it exceeds the integer limit.

Remember: Moreover, it is also helpful to know that even if the result of 4 times 2.5 is 10, the result will still be stored as a float because one of the operands is a float (2.5).

Integer rules:

  1. One digit is required for an integer.
  2. No decimal point in an integer.
  3. Integers can be either positive or negative.
  4. Binary (base 2), octal (base 8), decimal (base 10), and hexadecimal (base 16) notation can be used to specify integers.

Here are PHP’s predefined integer constants:

  1. PHP_INT_MAX – Supports the largest integer.
  2. PHP_INT_MIN – Supports the smallest integer.
  3. PHP_INT_SIZE – The byte size of an integer.

In PHP, you can check a variable’s type by using these functions:

  1. is_int()
  2. is_integer() – alias of is_int()
  3. is_long() – alias of is_int()

Below is an example that demonstrates the use of the is_int() function:

Example: 

<?php $a = 29; var_dump(is_int($a)); $a = 12.58; var_dump(is_int($a)); ?>

The following example checks whether a given variable is an integer using the is_int() function under the if_else statement:

Example: 

<?php $a = 12; $b = '19';//Checking $a as integer if(is_int($a)){ // This print the if statement because the $a is an integer echo "This is an integer <br>"; }else{ echo "This is not an integer <br>"; }//Checking $b as integer if(is_int($b)){ // This prints an else statement because the value of $b is string. echo "This is an integer <br>"; }else{ echo "This is not an integer <br>"; }?>

PHP Numbers


PHP Floats

The term float refers to a PHP number with a decimal point at the end or an exponential function.

2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floating point numbers.

There can be a maximum precision of 14 digits with the float data type.

This data type can store a value up to 1.7976931348623E+308 (platform dependent).

There are several predefined constants in PHP that are used for float values (from PHP 7.2):

  • PHP_FLOAT_MAX – A floating point number that represents the largest possible value.
  • PHP_FLOAT_MIN – A floating point PHP number that represents the smallest possible value.
  • – PHP_FLOAT_MAX – A floating point number that represents the smallest negative value.
  • PHP_FLOAT_DIG – Number of decimal digits that can be rounded to floats without losing precision.
  • PHP_FLOAT_EPSILON – The smallest positive number a, so that a + 1.0 != 1.0.

In PHP Numbers, you can check whether variables are floats by using the following functions:

  • is_float()
  • is_double() – alias of is_float()

Below is an example that demonstrates the use of the is_float() function:

Example: 

<?php $a = 29.921; var_dump(is_float($a)); ?>

In the below example we are checking whether PHP number is a float or integer:

Example: 

<?php $a = 15; $b = 41.34; var_dump(is_float($a)); //This prints the integer value var_dump(is_float($b)); //This prints the float value ?>

 


PHP Infinity

When a numeric value exceeds PHP_FLOAT_MAX, it is considered infinite.

Here are PHP functions to check if a number is finite or infinite:

  • is_finite()
  • is_infinite()

Data type and value are returned by the PHP var_dump() function

Below is an example that checks if a PHP number is finite or infinite:

Example: 

<?php $a = 1.9e478; var_dump($a); // This returns an infinite value ?>

The following example shows finite and infinite values:

Example: 

<?php $a = 1.7e485; $b = 1.25; var_dump($a); //This returns an infinite value var_dump($b); //This returns an finite value ?>

 


PHP NaN

A NaN is a Not a Number.

For mathematical operations that are impossible, NaN is used.

If you want to check if a value is not a PHP number, then you can use the following function:

  • is_nan()

Data type and value are returned by the PHP var_dump() function

Below is an example of using the acos() (Arc cos) function, If the calculation is invalid, a NaN value will be returned:

Example: 

<?php $a = acos(78); var_dump($a); ?>

Below is an example of using the asin() (Arc sine) function, If the calculation is invalid, a NaN value will be returned:

Example: 

<?php $a = asin(78); var_dump($a); ?>

PHP Numerical Strings

If we are talking about PHP numbers then, we can use the PHP is_numeric() function to determine whether a variable is a numeric value or not.

If the variable is either a number or a numeric string, the function will return true, otherwise, it will return false.

Below is an example that checks each $a, if it is numeric:

Example: 

<?php $a = 4878; var_dump(is_numeric($a)); $a = "4878"; var_dump(is_numeric($a)); $a = "48.78" + 50; var_dump(is_numeric($a)); $a = "car"; var_dump(is_numeric($a)); ?>

Below is an example that checks each $b, if it is numeric:

Example: 

<?php $b = "3578"; var_dump(is_numeric($b)); $b = 3578; var_dump(is_numeric($b)); $b = "bike"; var_dump(is_numeric($b)); $b = "35.78" + 110; var_dump(is_numeric($b)); ?>

Remember: As of PHP 7.0, the is_numeric() function will return FALSE for numeric strings in hexadecimal form, e.g. 0xf4c3b00c, since hexadecimal strings are no longer considered numeric strings.


Cast Strings and Floats to Integers

A numerical value can sometimes be converted into another data type.

A value can be converted to an integer using (int), (integer), or intval().

In the below example, floats and strings are converted to integers using the following method:

Example: 

<?php // Cast float to int $a = 3455.768; $cast_to_int = (int)$a; echo $cast_to_int; echo "";// Cast string to int $a = "3455.768"; $cast_to_int = (int)$a; echo $cast_to_int; ?>

Below is an another example, that floats and strings are converted to integers using the following method:

Example: 

<?php // Cast string to int $b = "4774.6465"; $str_to_int = (int)$b; echo $str_to_int;// Cast float to int $b = 4774.6465; $float_to_int = (int)$b; echo $float_to_int; echo ""; ?>

Example Explanation:

In above example we performs typecasting of a float and a string to an integer using the (int) operator.

  1. We have defined a variable $b and assigns it a value of 3874.6465, which is a floating-point PHP number.
  2. Then we perform typecasting by using the (int) operator, which converts the float value to an integer. The resulting value is stored in a variable called $cast_to_int.
  3. Then we have used echo statement to display the value of $cast_to_int on the screen. Since the original value was a float, the resulting value will be truncated to an integer, which is 3874.
  4. Then we have reassign the variable $b to a string with the same value, “3874.6465”.
  5. Again we perform typecasting using (int), this time on the string value of $b. The resulting value is stored in $cast_to_int.
  6. Finally, we have used the echo statement to display the value of $cast_to_int on the screen. Since the original value was a string, typecasting will attempt to convert the string to an integer, and will remove any non-numeric characters. In this case, the resulting value is 3874, which is the integer part of the original string.

 

Above example demonstrates how to perform typecasting of a float and a string to an integer in PHP using the (int) operator, and shows how the resulting value is affected by the original data type.

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *