Current Location: Home> Latest Articles> PHP Integer Division and How to Get Quotients

PHP Integer Division and How to Get Quotients

M66 2025-11-01

PHP Integer Division and Getting Quotients

Introduction

In PHP, integer division is used to calculate the result of dividing two numbers. By default, the / operator returns a floating-point quotient. If an integer quotient is needed, the intdiv() function can be used.

Syntax

The basic syntax is as follows:

$quotient = $dividend / $divisor;

Parameters:

  • $quotient: Variable to store the result
  • $dividend: The number to be divided
  • $divisor: The number to divide by

Example

For example, to calculate the quotient of 12 divided by 5:

$quotient = 12 / 5;
echo $quotient; // Output: 2.4

Precautions

  • The / operator returns a floating-point quotient
  • If an integer result is required, use the intdiv() function, e.g., intdiv(12,5) returns 2
  • Dividing by 0 will cause an error, so always check the divisor