PHP Int is a numerical value that can be assigned to the PHP variable. This means a number from the set -3, -2, -1, 0, 1, 2, 3. In the following few lines, I will specify the PHP integer types which can be set in decimal, octal, hexadecimal, and binary.

The int word refers to an integer, two both words are correct. Let’s focus on the integer types with examples.

Table Of Contents

Before getting started, there are some constants in PHP that are defining the size of the integers in PHP, maximum and minimum. Let’s see that.

  • The “PHP_INT_SIZE” used to get the PHP integer size by bytes.
  • The “PHP_INT_MAX” used to get the maximum size of the integer.
  • The “PHP_INT_MIN” used to get the minimum size of the integer.

The following code shows you an example for each one.

    echo PHP_INT_SIZE; // 8
    echo PHP_INT_MAX;  // -9223372036854775808
    echo PHP_INT_MIN;  // 9223372036854775807

As you know, the size of the integer is already depending on the platform. This means the maximum size of an integer value is 32 bit which means 2 billion can be utilized in an integer value as a maximum value.

PHP displays the integer values in 4 types. check the bellow figure.

PHP Int Types

Let’s explain each one in detail with an example.

PHP Int Decimal Numbers

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

PHP supports the underscore between digits to group them.

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

In the next section, I am going to show you the hexadecimal numbers in-depth.

PHP Int Hexadecimal Numbers

In general, hexadecimal is numerals between 0 and 15 base(16). In addition, it represents only single numbers between ( 0 – 9 ) but the other two numerical are replaced with the alphabetic characters from (a – f ) according to the index of alphabetic.

The following list shows you the double digits that would replaced with the target alphabetics.

  • The digit 10 is similar to the alphabet (A).
  • The digit 11 is similar to the alphabet (B).
  • The digit 12 is similar to the alphabet (C).
  • The digit 13 is similar to the alphabet (D).
  • The digit 14 is similar to the alphabet (E).
  • The digit 15 is similar to the alphabet (F).

So the hexadecimal number system between (0-9) and (A-F). Otherwise, you will not able to compile the result consisting of numbers and alphabet directly. Unless adding “0x” in the beginning.

  echo 0x2FD; // decimal 765

As you saw the result is 765. If you re-read this article you will see that we are depending on base(16) for the hexadecimal system. This means if we need to convert a number from hexadecimal to digits the calculation will be like in the below figure.

Hexadecimal Number System

Analyze the Result

In the red circles, there are 3 numbers starting from the right and ending on the left 0, 1, and 2. These are referring to the exponent and it is starting with zero. The exponent 162 means ( 16 x 16 ) or if it 164 means ( 16 x 16 x 16 x 16 ) and so on.

Let’s explain the above figure line by line from the left.

  1. The “0x” is referring to the hex code and these two chars should be added to define the hexadecimal numbers.
  2. In the next line, the numbers should take indexes from right to left and it would start with zero numbers as we mentioned in the previous section.
  3. So from the left, the number of ( 2 x 16 ) 2 : it extracted to ( 2 x ( 16 x 16) ). So the result was 512.
  4. The next number was ( 15 x 16 ) 1 : it extracted to ( 15 x ( 16 ) ). So the result was 240.
  5. The following number was ( 13 x 16 ) 0 : it extracted to ( 13 x ( … ) ). So the result was 13.
  6. To calculate the sum of the red font results it would give us the number 765.

Anyway, you understood the basics of how to calculate the exponent. You have to use the same calculation for the next PHP integer types. Let’s see the Octal numbers

PHP Int Octal Numbers

There is a range for digits in octal numbers between ( 0 – 7 ). Which are eight numbers “base(8)”. To define the octal numbers just add “0” Or (“0o” for PHP 8.1.0) in the beginning.

   echo 0457; // decimal 303 

The below figure shows you the result and how that would be calculated.

Octal Number System

According to the previous section, you have to use the same exponentiation calculation. Let’s get dive more into the binary numbers.

Binary Numbers

The binary number is only ( 0s and 1s ) there are no numbers else. Binary always, starting with “0b“. It is called binary bits. these numerals are used in the machine code – ( The Machine Language ) that is already used in the assembly language.

Here, we are going to cover these numeral systems and how they would be calculated in the PHP programming language.

   echo 0b101111; // Decimal 47

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

Binary Number System

The figure analysis.

In the first row, the figure contains the basic numerals that are starting from the left and end on the right. Always the binary system is starting with – “0b” in PHP. So without these two chars, it will be a normal number.

Also, the exponent would be started from 1 and increased from the right to the left and every time should be has a double number value.

Let’s see the numbers from the right side: ( 1 x 1 ) + ( 1 x 2 ) + ( 1 x 4 ) + ( 1 x 8 ) + ( 0 x 16 ) + ( 1 x 31 ) = 47.

Wrapping UP

You understood the difference between types of integers such as HexaDecimal, Octal, Binary, and decimal. In addition, you can convert from octal, hex, binary, or decimal to other Int types. To see more examples check the PHP data Types tutorial.

To see more details about data types check php manual.