PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
106: Basic Math Functions: round, ceil, floor
I was working on a shipping module for a client yesterday and ran into a classic problem: decimals. Specifically, I had a package weight of 4.3 kilograms, but the courier's API requires whole numbers for certain billing tiers. I can't just cast it to an (int) because that just chops the decimal off, and depending on the business rule, that might actually be the wrong move.
The problem with simple casting
First, I tried the quickest way to get rid of the decimal. I just cast the float to an integer. Let's see what happens here:
$weight = 4.7;
echo (int)$weight; // Output: 4
Wait. If the package is 4.7kg, and I tell the courier it's 4kg, I'm essentially stealing 0.7kg of capacity. My client will lose money. Casting to an int effectively always rounds down, regardless of how close we are to the next whole number. I need something more intentional.
Trying to be "fair" with round()
Most of the time, when we think of rounding, we think of the standard math class rule: .5 and up goes up, everything else goes down. PHP has a built-in for this: round().
$weight = 4.3;
echo round($weight); // Output: 4
$weight = 4.6;
echo round($weight); // Output: 5
That works for a general average, but in logistics, "fair" isn't usually how billing works. If a package is 4.1kg, the courier is still going to charge me for a 5kg slot because it doesn't fit in the 4kg box. round() is too lenient here. I need a way to force the number up, no matter how small the decimal is.
Forcing the ceiling
This is where ceil() comes in. I remember the term "ceiling"βit's the top of the room. It pushes any decimal, no matter how tiny, up to the next highest integer.
$weight = 4.1;
echo ceil($weight); // Output: 5
$weight = 4.0;
echo ceil($weight); // Output: 4
Perfect. If there is even a fraction of a gram over 4, ceil() bumps it to 5. This is exactly what the courier wants. It's a "safe" way to ensure we never under-calculate the space or cost required.
Ignoring the leftovers with floor()
Now, let's flip the script. Suppose I'm calculating how many full 1kg weights I can fit into a shipping crate. If I have 4.9kg of material, I only have 4 full kilograms; that last 0.9 doesn't count as a complete unit.
I could use (int) casting like I did at the start, but in my experience, using floor() makes your intent much clearer to the next developer reading your code. It says, "I am intentionally rounding down to the floor."
$material = 4.9;
echo floor($material); // Output: 4
It's the exact opposite of ceil(). While round() is a gamble based on the .5 threshold, ceil() and floor() are absolute. One always goes up, one always goes down.
A quick note on precision
Before I move on, I should mention that round() is the only one of these three that lets you specify precision. If I didn't want a whole number, but maybe just two decimal places for a price, I can do this:
$price = 12.5678;
echo round($price, 2); // Output: 12.57
ceil() and floor() don't do that; they always return a float that represents a whole integer. If you need to "ceil" to two decimal places, you'll have to get a bit more creative with multiplication and division, but for basic whole-number logic, these three are your bread and butter.
π Practical Task
Build a Warehouse Shelf Capacity Calculator
You are writing a script to help a warehouse manager organize items on a shelf. Create a PHP script that does the following:
- Define a variable
$totalItemWidthwith a decimal value (e.g.,15.3). - Define a variable
$shelfSlotWidthwith a value of5. - Calculate how many full slots are completely filled by the items using
floor(). - Calculate the total number of slots that will be occupied (even if the last slot is only partially filled) using
ceil(). - Print both results with descriptive labels.
There are no comments for now.