PHP Integer Data Type


Definition and Usage

In PHP, Integer is a scalar data type that represents a numeric constant represents a whole number wihout any fractional part. PHP allows an integer to be expressed in decimal, hexadecimal, octal or binary number system by prefixing appropriate symbol.

By default Integer is assumed in decimal notation. For Hexadecimal, octal and binary number system, respectively 0x, 0 an 0b symbols are prefixed.

Syntax

<?php
//Literal assignment of integer value to variable
$var=232; // Decimal
$var1=045; // Octal
$var2=oxB2; //hexadecimal
$var3=0b1001; //binary
?>

For better readibility, integer literal may use "_" as separation symbol which will be omitted by PHP scanner while processing.

<?php
$var=55_467; // it will treated as 55467
?>

PHP Version

Use of separation symbol "_" is avilable since PHP 7.40

Following example shows integer literal representation in different notations.

Example

 Live Demo

<?php
$var=10;
echo "decimal : " .$var . "
"; //Octal number $var1=010; echo "Octal: " . $var1 ."
"; //Hexadecimal number $var2=0x10; echo "hexadecimal : " . $var2 . "
"; //binary number $var3=0b10; echo "binary : " .$var3; ?>

Output

This will produce following result −

decimal : 10
Octal: 8
hexadecimal : 16
binary : 2

This Example uses separation symbol

Example

<?php
$var=1_45_690;
echo $var . "
"; ?>

Output

This will produce following result −

145690

If the integer is beyond allowed range, it is converted to floating point

Example

 Live Demo

<?php
$var=PHP_INT_MAX+1;
var_dump($var);
?>

Output

This will produce following result −

float(9.2233720368548E+18)

Updated on: 19-Sep-2020

379 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements