CKTIS / PHP
Sum with assignment (+=)
Add the delivery charge to the order total and store the new amount.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
$value += $operandAdd the delivery charge to the order total and store the new amount. For two arrays, only missing keys are added to the left array.
With integer operands a result within the integer range stays an integer. Overflow can produce a float and lose precision. In PHP 8 adding to a numeric string converts it to a number. Text after a numeric prefix triggers a warning, and a string without a numeric prefix raises TypeError.
Evaluation
$value = 16; $value += 5;→21$value = (-4); $value += 4;→0$value = 3; $value += 0;→3$value = '12.5'; $value += 2; echo json_encode([$value, get_debug_type($value)]);→[14.5,"float"]$value = ['a'=>3]; $value += ['a'=>9,'b'=>4];→{"a":3,"b":4}$value = 'free'; $value += 2;→TypeError
Examples
<?php
$value = 16;
$value += 5;
echo json_encode($value), PHP_EOL;
$value = (-4);
$value += 4;
echo json_encode($value), PHP_EOL;
$value = 3;
$value += 0;
echo json_encode($value), PHP_EOL;
$value = '12.5';
$value += 2;
echo json_encode([$value, get_debug_type($value)]), PHP_EOL;
$value = ['a'=>3];
$value += ['a'=>9,'b'=>4];
echo json_encode($value), PHP_EOL;
$value = 'free';
try {
$value += 2;
} catch (TypeError $error) {
echo get_class($error), PHP_EOL;
}
Result
21
0
3
[14.5,"float"]
{"a":3,"b":4}
TypeError