<?php
$variabel = "globalt scope";
function hentscope($scope) {
if($scope === "getglobal") {
global $variabel;
} elseif($scope === "GLOBAL") {
$variabel = $GLOBALS["variabel"];
} elseif($scope === "local") {
$variabel = "lokalt scope";
}
return $variabel;
}
// vi kalder så på forskellige måder
echo hentscope("getglobal"); // her henter vi variabel fra det globale scope
echo hentscope("GLOBAL"); // henter gennem $GLOBALS (superglobal)
echo hentscope("local"); // henter variabel fra local scope (sætter egen variabel) - denne skal ikke bruges i dette tilfælde
echo hentscope(); // sucky error, der er ingen variabel i dit scope der hedder variabel
?>
globalt scopeglobalt scopelokalt scope
Fatal error: Uncaught ArgumentCountError: Too few arguments to function hentscope(), 0 passed in /in/9SCYi on line 22 and exactly 1 expected in /in/9SCYi:5
Stack trace:
#0 /in/9SCYi(22): hentscope()
#1 {main}
thrown in /in/9SCYi on line 5
Process exited with code 255.
globalt scopeglobalt scopelokalt scope
Warning: Missing argument 1 for hentscope(), called in /in/9SCYi on line 22 and defined in /in/9SCYi on line 5
Notice: Undefined variable: scope in /in/9SCYi on line 6
Notice: Undefined variable: scope in /in/9SCYi on line 8
Notice: Undefined variable: scope in /in/9SCYi on line 10
Notice: Undefined variable: variabel in /in/9SCYi on line 14
Output for 5.0.4 - 5.0.5
globalt scopeglobalt scopelokalt scope
Warning: Missing argument 1 for hentscope() in /in/9SCYi on line 5
Notice: Undefined variable: variabel in /in/9SCYi on line 14
globalt scopeglobalt scopelokalt scope
Warning: Missing argument 1 for hentscope() in /in/9SCYi on line 5
Notice: Undefined variable: variabel in /in/9SCYi on line 14