<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>php</title>
</head>
<body>
<?php
echo "helo";
$globVar =100; //global scope
$global2 = 10;
function sum(){
echo 5+2;
}
function tambah($x,$y){
echo "<br>";
echo $x +$y;
//echo "global var". $globVar; -> bakal error. butuh keyword global
return $x +$y;
}
function changeGlobalVar($x){
echo "<br>";
global $globVar;
return $x + $globVar;
}
function substraction_with_global_keyword(){
$GLOBALS['globVar'] = $GLOBALS['global2'] + $GLOBALS['global2'];
}
function test_static(){
static $fee = 200;
$fee = $fee + $fee;
echo "<br>";
echo $fee;
}
sum(); //7
tambah(10,20); //30
echo changeGlobalVar(20); //120
echo "<br>";
echo $globVar; //100
//use global keyword.
echo "<br>";
substraction_with_global_keyword();
echo $globVar; //20
//test static var
echo "<br>";
test_static(); //400
test_static(); //800
?>
</body>
</html>
No comments:
Post a Comment