Home ยป PHP Integers Tutorial with Code Examples

PHP Integers Tutorial with Code Examples

In PHP, an integer is a whole number without any decimal point. It can be positive, negative, or zero.

Integers are one of the most commonly used data types in PHP, especially when performing mathematical operations or representing values such as counts, ages, and IDs.

In this tutorial, we will cover:

1. Introduction to Integers

An integer is any number that:

Is a whole number (no fractions or decimals)

Can be positive, negative, or zero

PHP supports integer values, which can be used in arithmetic, comparison, and logical operations.

PHP automatically handles the type conversion, so you don’t need to declare the type of a variable.

2. Defining Integers in PHP

To define an integer, you simply assign a whole number to a variable.

Example:

<?php
$age = 30; // Positive integer
$balance = -500; // Negative integer
$zero = 0; // Zero, which is also an integer
?>

 

In this example, $age, $balance, and $zero are all integers.

3. Properties and Limits of Integers

Integer Size

PHP integers are signed and can store values up to a certain limit, depending on your system’s architecture (32-bit or 64-bit).

32-bit system: The range is from -2,147,483,648 to 2,147,483,647.
64-bit system: The range is much larger, from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

You can use the built-in constant PHP_INT_MAX to check the maximum integer size on your system.

Example:

<?php
echo PHP_INT_MAX; // Outputs the maximum integer value on your system
echo PHP_INT_MIN; // Outputs the minimum integer value on your system
?>

Overflow

When an integer exceeds the limit of the platform, it is automatically converted to a floating-point number (float).

Example:

<?php
$largeNumber = PHP_INT_MAX + 1;
echo $largeNumber; // Outputs the number as a float
?>

 

4. Integer Operations

PHP supports all basic arithmetic operations with integers, including addition, subtraction, multiplication, and division.

Arithmetic Operations:

Addition (+): Adds two integers.
Subtraction (-): Subtracts one integer from another.
Multiplication (*): Multiplies two integers.
Division (/): Divides one integer by another.
Modulus (%): Returns the remainder of division.

Example:

<?php
$x = 10;
$y = 3;

echo $x + $y; // Outputs: 13
echo $x - $y; // Outputs: 7
echo $x * $y; // Outputs: 30
echo $x / $y; // Outputs: 3.33333
echo $x % $y; // Outputs: 1 (remainder of 10 / 3)
?>

5. Type Checking and Conversion

Checking if a Variable is an Integer

To check if a variable is an integer, use the is_int() or is_integer() function. These functions return true if the value is an integer and false otherwise.

Example:

<?php
$var1 = 100;
$var2 = 12.5;

if (is_int($var1)) {
echo "$var1 is an integer"; // Outputs: 100 is an integer
}

if (!is_int($var2)) {
echo "$var2 is not an integer"; // Outputs: 12.5 is not an integer
}
?>

 

Converting to Integer

If you want to convert another data type (like a string or float) to an integer, you can cast it using (int) or intval().

Example:

<?php
$floatVar = 45.78;
$intVar = (int) $floatVar; // Cast float to integer
echo $intVar; // Outputs: 45

$stringVar = "123";
$intVar = intval($stringVar); // Convert string to integer
echo $intVar; // Outputs: 123
?>

 

In the example above, (int) converts the floating-point number 45.78 to 45, and intval() converts the string “123” to the integer 123.

6. Common Uses of Integers

Looping with Integers

Integers are often used in loops to define counters and limits.

Example (For Loop):

<?php
for ($i = 1; $i <= 5; $i++) {
echo $i; // Outputs: 12345
}
?>

Counting Items

Integers are frequently used to count items in an array or elements in a collection.

Example:

<?php
$fruits = array("Apple", "Banana", "Orange");
$count = count($fruits); // Returns the number of elements in the array
echo $count; // Outputs: 3
?>

Conditional Statements

You can compare integers in conditional statements to determine the flow of a program.

Example (If-Else):

<?php
$temperature = 30;

if ($temperature > 25) {
echo "It's a hot day!";
} else {
echo "It's a cool day!";
}
// Outputs: It's a hot day!
?>

 

7. Example Use Cases

Integer in Shopping Cart Total Calculation:

<?php
$priceOfItem1 = 50;
$priceOfItem2 = 30;
$quantityItem1 = 2;
$quantityItem2 = 1;

$total = ($priceOfItem1 * $quantityItem1) + ($priceOfItem2 * $quantityItem2);
echo "Total Price: $" . $total; // Outputs: Total Price: $130
?>

 

Integer in Age Calculation:

<?php
$currentYear = 2024;
$birthYear = 1990;

$age = $currentYear - $birthYear;
echo "You are " . $age . " years old."; // Outputs: You are 34 years old.
?>

 

Integer in Bank Balance Validation:

<?php
$balance = 500;
$withdrawAmount = 200;

if ($balance >= $withdrawAmount) {
$balance -= $withdrawAmount;
echo "Withdrawal successful. New balance: $" . $balance; // Outputs: Withdrawal successful. New balance: $300
} else {
echo "Insufficient funds.";
}
?>

 

Conclusion

In this tutorial, we explored the integer data type in PHP, from its definition to its limits, operations, and use cases. Integers are fundamental in many aspects of programming, such as counting, looping, and performing calculations.

Key points to remember:

Integers are whole numbers (positive, negative, or zero).
PHP supports all basic arithmetic operations with integers.
Use is_int() to check if a variable is an integer, and (int) or intval() to convert other types to integers.
Integers are widely used in loops, conditions, and calculations.
By mastering integers, you’ll be able to write more efficient and meaningful PHP programs!

You may also like