In PHP, an integer (int) is a data type that represents whole numbers without any decimal point. It can be a positive or negative whole number or zero. PHP integers can be used to perform arithmetic operations, comparisons, and other numerical tasks in your code.

The term ‘int’ refers to an integer in PHP, and both words are correct. Let’s focus on integer types with examples.

Before getting started, there are constants in PHP that define the size limits of integers, both maximum and minimum. Let’s take a look at them.

  • PHP_INT_SIZE: This constant represents the size of an integer in bytes on the current platform.
  • PHP_INT_MAX: This constant holds the maximum value for integers on the current platform.
  • PHP_INT_MIN: This constant holds the minimum value for integers on the current platform.

Let’s see examples for each of them.

The following code shows you an example for each one.

// Example using PHP_INT_SIZE
$intSize = PHP_INT_SIZE; // 8
echo "Size of an integer on this platform: $intSize bytes\n";

// Example using PHP_INT_MAX
$maxInt = PHP_INT_MAX; // 9223372036854775807
echo "Maximum value for integers on this platform: $maxInt\n";

// Example using PHP_INT_MIN
$minInt = PHP_INT_MIN; // -9223372036854775808
echo "Minimum value for integers on this platform: $minInt\n";

As you may know, the size of an integer (int) in PHP depends on the platform. This implies that the maximum size of an integer value is 32 bits, allowing a maximum value of 2 billion to be stored in an integer variable.

PHP Integer( Int ) Types

Anyway, PHP has 4 integer (int) types, which are:-

  • Decimal
  • Hexadecimal
  • Octal
  • Binary

Let’s focus on each one in-depth.

Exploring Decimal Numbers in PHP

Decimal numbers can be defined as a number that is separated by a decimal point. In other words, the decimal is located to the right of the number, and it can consist of 1 digit, 2 digits, or more. The maximum number of digits after the decimal point is 10 in base 10.

PHP supports the use of underscores between digits to group them. For example:

   echo 4_487_456 // 4487456 decimal;
   echo 548 // 548 decimal;

Let’s proceed to the next section to delve deeper into hexadecimal numbers.

Understanding Hexadecimal Numbers

In general, hexadecimal refers to numerals between 0 and 15 in base 16. It represents single-digit numbers from 0 to 9, and the remaining six digits are replaced with alphabetical characters from ‘a’ to ‘f’ based on their respective indices.

The following list shows you the double digits that would be replaced with the corresponding alphabetic characters:

  • 10 -> a
  • 11 -> b
  • 12 -> c
  • 13 -> d
  • 14 -> e
  • 15 -> f

In hexadecimal notation, these substitutions help represent values beyond single-digit numbers.”

To better understand the hexadecimal number system, distinguish between (0-9) and (A-F). Otherwise, you will not be able to compile results consisting of both numbers and alphabets directly—unless you add ‘0x’ at the beginning.

To clarify that, check the example below:

<?php  echo 0x2FD; // decimal 765 ?>

In this example, the result is 765. If you reread this section, you will notice that we are relying on base-16 for the hexadecimal system. This implies that if we need to convert a number from hexadecimal to digits, the calculation will be as shown in the figure below.

Hexadecimal Number System

Explaining the Calculation Process

In the red circles, mark three numbers from right to left: 0, 1, and 2. These numbers represent the exponent and begin with zero. For instance, an exponent of 162 signifies (16 x 16), and an exponent of 164 signifies (16 x 16 x 16 x 16), continuing in this manner.

Let’s break down the figure line by line, starting from the left.

  • To define hexadecimal, add “0x” these two characters in the beginning of numbers.
  • InIn the subsequent line, you should index the numbers from right to left, starting with zero, as discussed in the previous section.
  • From the left, expand the first number as (2 x 16)₂, which further expands to (2 x (16 x 16)). The result is 512.
  • The next number, (15 x 16)₁, expands to (15 x (16)). The result is 240.
  • Following that, the number (13 x 16)₀ expands similarly. The result is 13.
  • Adding up these red font results gives us the number 765.

By now, you’ve grasped the basics of calculating exponents. The same approach applies when working with other PHP integer (int) types. Next, let’s explore Octal numbers.

Anyway, let’s move on to Octal numbers in PHP.

Octal Numbers in PHP

There is a digit range for octal numbers between 0 and 7, encompassing eight numbers in total (base 8). To define octal numbers, simply add ‘0’ or (‘0o’ for PHP 8.1.0) at the beginning. For example:

   echo 0457; // decimal 303 

The figure below provides an explanation and illustrates how it would be calculated.

Octal Number System

Explaining the Calculation Process

The number 0457 is an octal representation. In the octal system, the digits extend from 0 to 7. The equivalent decimal value is calculated as follows:

4 * (8^2) + 5 * (8^1) + 7 * (8^0) = 303

Therefore, echo 0457; will output 303 in decimal notation.

Let’s delve deeper into another type of PHP integers—binary numbers.

Understanding Binary Numbers

The binary number system consists only of 0s and 1s; there are no other numerical digits. Binary numbers always start with ‘0b’ and are referred to as binary bits. These numerals are extensively used in machine code, which is the machine language utilized in assembly language programming.

In this section, we will cover these numeral systems and explain how they are calculated in the PHP programming language.

<?php echo 0b101111; // Decimal 47 ?>

The figure below shows how to calculate the binary numeral system.

Binary Number System

the binary number 0b101111 is being echoed. The 0b prefix indicates that the following digits are in binary notation. The binary number 101111 represents a series of powers of 2:

1 * 2^5 + 0 * 2^4 + 1 * 2^3 + 1 * 2^2 + 1 * 2^1 + 1 * 2^0

Let’s calculate each term:

32 + 0 + 8 + 4 + 2 + 1 = 47

So, 0b101111 in binary is equal to 47 in decimal. The echo statement outputs this decimal value.

I hope this article has been helpful for you. Let’s summarize it.

Wrapping UP

this comprehensive guide has provided a detailed exploration of various PHP integer types, including decimal, hexadecimal, octal, and binary numbers. We’ve covered the fundamentals, syntax, and practical examples for each numeral system.

Firstly, we delved into decimal numbers, showcasing their representation and usage in PHP, including the support for underscores between digits for better readability.

Moving on, we explored the hexadecimal number system, understanding its unique representation and the conversion process between hexadecimal and decimal. The inclusion of examples and explanations aimed to enhance comprehension.

Next, we ventured into octal numbers, uncovering the digit range and the conventions for defining octal numbers in PHP. Real-world examples were provided to illustrate the concepts discussed.

The article then transitioned to binary numbers, elucidating their fundamental nature as sequences of 0s and 1s and their prominent role in machine code and assembly language programming.

Throughout the discussion, constants like PHP_INT_SIZE, PHP_INT_MAX, and PHP_INT_MIN were highlighted, offering insights into the size limitations of integers on different platforms.

Additionally, the article demonstrated practical coding examples for better understanding, ensuring that readers can grasp the concepts and apply them in real-world scenarios.