PHP Integer
In this chapter you will learn:
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:
- What is float number
- PHP Float Literal
- Example - floating-point arithmetic
- Example - Calculate the area of a circle of given radius