Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword global

Inside a function, bind a local name by reference to the global variable of the same name.

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

Syntax

global $count;

Inside a function, bind a local name by reference to the global variable of the same name. Assignment through this binding changes the global value. If the global does not exist it is created with null. Unsetting the local binding leaves the global variable present.

$count=2; function advance(){global $count,$memo; ++$count; unset($count);}
advance(); echo json_encode([$count,array_key_exists("memo",$GLOBALS),$GLOBALS["memo"]]);
[3,true,null]

Examples

<?php
$count=2; function advance(){global $count,$memo; ++$count; unset($count);}
advance(); echo json_encode([$count,array_key_exists("memo",$GLOBALS),$GLOBALS["memo"]]);

Result

[3,true,null]

Sources