PHP Basics · Lesson 4 of 8
Integers
Work with whole numbers and integer-safe operations.
Lesson
Integers represent whole numbers, including negative values. PHP may convert a value to a float when an operation cannot be represented as an integer, so validate user input instead of relying on implicit conversion. Use intdiv when you need whole-number division and the remainder operator when you need what is left over.
The example splits 17 items into groups of five and keeps the remainder explicit.
Further guidance
Inspect a value before relying on an implicit conversion. Keep input validation separate from casting, use strict comparisons when type matters, and choose an explicit representation for values such as money or optional data. Try normal, empty, and unexpected inputs so the result is deliberate.
Practice task: Create a fresh example for Integers. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.
Code examples
Integers
PHP
$items = 17;
$groupSize = 5;
echo intdiv($items, $groupSize), ' full groups; ', $items % $groupSize, ' left';
Expected output
3 full groups; 2 left
Key points
- Work with whole numbers and integer-safe operations.
- Validate external data before converting or using it.
- Use strict comparisons when both value and type matter.
Your learning progress
Finish this lesson
Optional practice
PHP playground
This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.
Execution is disabled. No code is sent to a server.