CKTIS / PHP
$GLOBALS
Via de superglobale array $GLOBALS zijn globale variabelen in elke scope op naam bereikbaar.
Versie: PHP 8.4; repository runtime, production runtime not attested
Syntax
$GLOBALS["count"] += 1;Via de superglobale array $GLOBALS zijn globale variabelen in elke scope op naam bereikbaar. Een toewijzing aan een element wijzigt de globale variabele ook als er een gelijknamige lokale variabele is. Sinds PHP 8.1 mag de hele array niet worden vervangen of per referentie worden gebonden. Een scalaire waarde in een kopie kan onafhankelijk veranderen.
$count = 2;
function update(): array {
$count = 50;
$GLOBALS["count"] += 3;
$snapshot = $GLOBALS;
$snapshot["count"] = 90;
return [$count, $snapshot["count"]];
}
echo json_encode([update(), $count]);
[[50,90],5]
Voorbeelden
<?php
$count = 2;
function update(): array {
$count = 50;
$GLOBALS["count"] += 3;
$snapshot = $GLOBALS;
$snapshot["count"] = 90;
return [$count, $snapshot["count"]];
}
echo json_encode([update(), $count]);
Resultaat
[[50,90],5]