bhylu.com

Advertisements

PHP Operators

Operators combine values into expressions. Parentheses and clear intermediate names make a lending calculation easier to review than relying on remembered precedence rules.

How it works

Multiplication and division bind more tightly than addition and subtraction. Concatenation has its own precedence, so group the arithmetic that belongs inside a message. Assignment changes a variable; equality compares values. Compound assignments such as += update the left-hand variable, but still require a sensible type and unit. Use intdiv when whole-number division is the actual requirement rather than silently assuming that / returns an integer.

Run it locally

Run this complete source locally with PHP 8.2 or newer. Save it using the filename shown and run the command below. PHP is not currently available in Bhylu’s browser playground. Apache and MySQL are not needed for an ordinary CLI example.

php -l operators.php
php operators.php

Download this source as a local PHP lab file. Keep lab files separate from the Bhylu project and production data.

Example

PHP 8.2+ · operators.php

<?php
declare(strict_types=1);

$boxes = 3;
$perBox = 5;
$total = $boxes * $perBox;
$total -= 2;
echo "Serviceable: " . $total . "\n";
echo intdiv($total, 4), " full groups\n";
echo $total % 4, " remaining\n";

Expected result

Serviceable: 13
3 full groups
1 remaining

The multiplication produces fifteen, then removing two leaves thirteen. Whole groups of four account for twelve tools and leave one. The separate variables expose the steps and units, which makes a failed test easier to diagnose than one compressed expression with several side effects.

Check the official PHP manual for detailed version and API contracts.