PHP Integer

In this chapter you will learn:

  1. What are integers
  2. Example - Add two integer together
  3. PHP integer literal
  4. Example - Integer literal

Description

Integers hold whole numbers, either positive or negative.

Example

The following code adds two integer values together in PHP.


<?php //from j a  va 2  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; 
?>

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


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

Example 2

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


<?PHP//from j a  va 2s .  c om
$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.

Next chapter...

What you will learn in the next chapter:

  1. What is float number
  2. PHP Float Literal
  3. Example - floating-point arithmetic
  4. Example - Calculate the area of a circle of given radius
Home » PHP Tutorial » PHP Data Types
PHP Data Types
PHP Boolean
PHP Integer
PHP Float
PHP Data Type Cast
PHP Automatic Type Conversion
PHP Super globals
PHP References
PHP Constants