Backend Development 3 min read

Using PHP ceil() Function: Syntax, Parameters, and Examples

The article explains PHP's ceil() function, detailing its syntax, parameter, and behavior with positive, negative, and integer values, and provides four code examples demonstrating rounding up numbers, while noting that the function only works with numeric data.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP ceil() Function: Syntax, Parameters, and Examples

PHP's ceil() function is a common mathematical function that rounds a numeric value up to the nearest integer.

Syntax:

ceil(float $value): float

Parameter: $value is the number to be rounded up; the function returns the rounded result.

Example 1:

$num1 = 5.3;<br/>$ceilNum1 = ceil($num1);<br/>echo $ceilNum1; // outputs 6

In this example, the value 5.3 is rounded up to 6.

Example 2:

$num2 = -3.8;<br/>$ceilNum2 = ceil($num2);<br/>echo $ceilNum2; // outputs -3

The function rounds the negative number -3.8 up to -3, preserving its sign.

Example 3:

$num3 = 7;<br/>$ceilNum3 = ceil($num3);<br/>echo $ceilNum3; // outputs 7

Since the value is already an integer, ceil() leaves it unchanged.

Example 4:

$num4 = 10.5;<br/>$ceilNum4 = ceil($num4);<br/>echo $ceilNum4; // outputs 11

The value 10.5 is rounded up to 11.

Note that ceil() can only be used with numeric data; applying it to other data types will cause an error.

Summary: The ceil() function is a practical tool for rounding numbers up to the nearest integer in PHP, and the provided examples illustrate its behavior with various inputs.

Backend DevelopmentPHPCode Exampleroundingmath functionceil
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.