In PHP development, the ceil() function is a very practical mathematical function for rounding floating point numbers upwards. In actual development, whether it is Laravel or ThinkPHP, the two mainstream frameworks, ceil() can provide us with convenient support for solving scenarios such as paging calculation, price rounding, inventory management, etc. This article will introduce in detail how to use the ceil() function in these two frameworks and give examples of practical applications.
The ceil() function is used to round a number upwards and return the smallest integer greater than or equal to the number. Its syntax is as follows:
ceil(float $value): float
For example:
echo ceil(3.2); // Output4
echo ceil(-1.1); // Output-1
As a modern PHP framework, Laravel provides rich tools and convenient syntax. In actual projects, the common use scenario of ceil() is to calculate the total number of pages of paging.
Suppose we have a database query that returns the total number of records, we need to calculate the total number of pages of paging:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class ProductController extends Controller
{
public function index(Request $request)
{
$perPage = 15; // Displayed per page15Data
$total = Product::count(); // Get the total number of products
$totalPages = ceil($total / $perPage); // Calculate the total number of pages
return view('products.index', compact('totalPages'));
}
}
In the Blade template, you can generate paging links based on $totalPages .
In addition, if you need to round up when processing the price, you can also use ceil() directly:
$price = 19.99;
$priceRounded = ceil($price); // 20
ThinkPHP also supports native PHP functions, and using ceil() is also very intuitive. The following example shows the calculation of the total number of pages paging in the controller:
<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Product extends Controller
{
public function index()
{
$perPage = 20; // Number of bars per page
$total = Db::name('product')->count(); // Get the total number of products
$totalPages = ceil($total / $perPage); // Calculate the number of pages
$this->assign('totalPages', $totalPages);
return $this->fetch();
}
}
In the template file, it can be used by {$totalPages} .
Suppose that in an e-commerce system, the price of goods needs to be uniformly rounded up and displayed. Regardless of the price, the front-end display must be an integer.
<?php
$prices = [12.1, 15.9, 22.3, 18.75];
$roundedPrices = array_map('ceil', $prices);
print_r($roundedPrices); // Output:[13, 16, 23, 19]
Combined with URL parameters dynamic paging calculation:
In Laravel:
<?php
$page = request()->get('page', 1);
$perPage = 10;
$total = Product::count();
$totalPages = ceil($total / $perPage);
$url = "https://m66.net/products?page=" . $page;
return view('products.index', compact('totalPages', 'page', 'url'));
ThinkPHP:
<?php
$page = input('get.page', 1);
$perPage = 10;
$total = Db::name('product')->count();
$totalPages = ceil($total / $perPage);
$url = "https://m66.net/products?page=" . $page;
$this->assign(compact('totalPages', 'page', 'url'));
return $this->fetch();
The use of PHP's ceil() function in Laravel and ThinkPHP frameworks is very direct and efficient, especially in pagination calculations and price processing. By combining it with the framework's database operations and view display, development efficiency and code simplicity can be greatly improved.
Whether it is Laravel's elegant syntax or ThinkPHP's flexible and lightweight, ceil() can meet the actual project needs well.