Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Value assignment (=)

Assignment stores the value of the right expression in the writable target on the left.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

$destination = $expression

Assignment stores the value of the right expression in the writable target on the left. The assignment expression itself evaluates to that value. Ordinary array elements separate on write after copying. An element explicitly held by reference still shares its value across both arrays. Assigning an object copies the handle, so both variables can observe property mutation.

$destination = $expression
[3,7,7,7,7]

Examples

<?php
$a = ['n'=>3];
$b = $a;
$b['n'] = 7;
$box = (object)['n'=>3];
$other = $box;
$other->n = 7;
$x = 1;
$linked = [&$x];
$copy = $linked;
$copy[0] = 7;
echo json_encode([$a['n'], $b['n'], $box->n, $linked[0], $x]);

Result

[3,7,7,7,7]

Sources