Header Ads

PHP 전역/지역변수,정적변수,가변변수



// 전역변수
$a = 1;
$b = 2;
function global_var()
{
 global $a;
 echo "\$a value is {$a}";
}
global_var();
unset($a);
unset($b);

// 지역변수
$a = 1;
$b = 2;
function local_var()
{
 $a = 2;
 echo "\$a value is {$a}";
}
local_var();
unset($a);
unset($b);

// global 키워드 사용
$a = 1;
$b = 2;
function sum1()
{
 global $a,$b;
 $b = $a + $b;
}
sum1();
echo "\$b value is {$b}";
unset($a);
unset($b);

// $GLOBALS 연관배열 사용
$a = 1;
$b = 2;
function sum2()
{
 $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];
}
sum2();
echo "\$b value is {$b}";
unset($a);
unset($b);

// 정적변수
function static_var()
{
 static $a = 10;
 echo "\$a value is {$a}";
 $a = $a + 1;
}
static_var();
static_var();
static_var();
echo "\$a value is {$a}";
unset($a);

// 가변변수
$a = "NEW";
${$a} = "OVERLOAD";
echo "a = " . $a;
echo "a = " . ${$a};
echo "a = " . $NEW;

No comments

Copyright 2016, minki. All rights reserved. Powered by Blogger.