PHP Integer

Description

Integers hold whole numbers, either positive or negative.

Example

The following code adds two integer values together in PHP.


<?php //from   ww w.  j  a  v  a2 s.c  o m
$var1 = 5; 
$var2 = 2; 
$answer = $var1 + $var2; 
print "$var1 plus $var2 equals $answer."; 
?>

The code above generates the following result.

Literal

We may specify integers in hexadecimal (base 16) or octal (base 8). The octal number system only uses the digits 0 to 7. hexadecimal uses 0 to 9, then A, B, C, D, E, and F.

To specify number in octal, we must precede it with a 0 (zero).


<?PHP
$octalnum = 06331; 
print $octalnum; 
?>

The code above generates the following result.

Hexadecimal

To specify a number in hexadecimal, precede it with 0x.


<?PHP
$hexnum = 0x44; 
print $hexnum; 
?>

The code above generates the following result.

Example 2

The following code uses different integer literals to assign value to integers.


<?PHP/* w w  w  .java  2  s . c o m*/
$a = 1234; # decimal number
print($a);
print("\n");
$a = -123; # a negative number
print($a);
print("\n");
$a = 0123; # octal number (equivalent to 83 decimal)
print($a);
print("\n");
$a = 0x12; # hexadecimal number (equivalent to 18 decimal)
print($a);
print("\n");
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone