PHP ceil() Function Tutorial 🎯

beginner
22 min

PHP ceil() Function Tutorial 🎯

Welcome to our comprehensive guide on the PHP ceil() function! In this lesson, we'll explore what ceil() does, why we use it, and how to use it in your PHP projects. Let's get started! πŸ“

Introduction πŸ’‘

The ceil() function in PHP is a built-in function that rounds a given number up to the nearest integer. It's useful when you want to ensure that a floating-point number is rounded up to the nearest whole number.

Syntax πŸ“

The syntax for the ceil() function is simple:

php
ceil($number);

Replace $number with the floating-point value you want to round up.

Examples πŸ’‘

Example 1: Rounding a simple number

php
<?php $number = 6.3; $rounded = ceil($number); echo "The rounded number is: $rounded"; ?> // Output: The rounded number is: 7

In this example, we have a floating-point number 6.3. By using the ceil() function, we round it up to the nearest integer, which is 7.

Example 2: Rounding a variable in a practical scenario πŸ’‘

php
<?php $price = 34.56; $discount = 20; $discount_amount = ($price * $discount) / 100; $final_price = ceil($price - $discount_amount); echo "The final price after discount is: $final_price"; ?> // Output: The final price after discount is: 27

In this example, we calculate a discount on a product price and then round the final price up to the nearest integer.

Quiz πŸ“

Quick Quiz
Question 1 of 1

What does the PHP `ceil()` function do?

Stay tuned for our next lesson where we'll explore the floor() function, its uses, and examples! πŸš€