PHP 변수를 스크립트로 사용하는 방법입니다.

· 12년 전 · 2588

<?php
// PHP변수 스크립트에서 쉽게 사용하기
function setScriptVar($name, $obj) {
global $script;
$script = "<script language='javascript'>";

if(is_object($obj)) {
$script .= "var $name = new Array();";
factory($name, $obj,'object');
} elseif(is_array($obj)) {
$script .= "var $name = new Array();";
factory($name, $obj,'array');
} else {
if(is_numeric($obj)) $script .= "var $name = $obj;";
else $script .= "var $name = '$obj';";
}
$script .= "</script>";
echo $script;
}

function factory($name, $data, $type = NULL) {
global $script;
foreach($data as $key => $val) {
if(is_object($val)){
factory($name.".".$key, $val, 'object');
} elseif(is_array($val)){
if($type == 'array') {
$script .= $name."[".$key."] = new Array();";
factory($name."[".$key."]", $val, 'array');
} else {
$script .= $name.".".$key." = new Array();";
factory($name.".".$key, $val, 'array');
}
} elseif(preg_match("/[a-zA-Z_][a-zA-Z0-9_]*/",$key) && $type == 'object') {
if(is_numeric($val)) $script .= $name.".".$key."=".$val.";";
else $script .= $name.".".$key."='".$val."';";
} elseif($type == 'array') {
if(!is_numeric($key)) $key = "'".$key."'";
if(is_numeric($val)) $script .= $name."[".$key."]=".$val.";";
else $script .= $name."[".$key."]='".$val."';";
}
}
}

// 사용법 setScriptVar(변수명지정,PHP변수);

// 일반변수
$market = "I Like Fruit!";
setScriptVar("market", $market);

// 배열
$fruit = array('Apple','Pineapple',array('Tomato','Banana'));
setScriptVar("fruit", $fruit);

// 오브젝트와 배열
$list->like = 'Pineapple';
$list->hate = 'Tomato';
$list->buy = array('Apple','Pineapple');
setScriptVar("list", $list);

// 변수타입
$num = array(100,'200');
setScriptVar("num",$num);
?>

<script>
//alert(market); // I Like Fruit!
//alert(fruit[0]+"/"+fruit[2][0]); // Apple/Tomato
//alert(list.like+"/"+list.buy[0]); // Pineapple/Apple
//alert(num[0] + 10); // 110
//alert(num[1] + 10); // 20010
</script>

첨부파일

php변수.txt (2 KB) 11회 2013-03-20 13:27
|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
12년 전 조회 4,934
12년 전 조회 9,795
12년 전 조회 5,389
12년 전 조회 4,975
12년 전 조회 2,576
12년 전 조회 2,664
12년 전 조회 4,387
12년 전 조회 5,205
12년 전 조회 5,619
12년 전 조회 2,820
12년 전 조회 2,536
12년 전 조회 2,146
12년 전 조회 2,190
12년 전 조회 3,974
12년 전 조회 3,955
12년 전 조회 2,521
12년 전 조회 4,035
12년 전 조회 2,514
12년 전 조회 9,696
12년 전 조회 2,793
12년 전 조회 5,023
12년 전 조회 7,614
12년 전 조회 2,799
12년 전 조회 4,098
12년 전 조회 1,807
12년 전 조회 2,488
12년 전 조회 2,365
12년 전 조회 2,548
12년 전 조회 4,554
12년 전 조회 2,319
12년 전 조회 3,051
12년 전 조회 2,478
12년 전 조회 2,145
12년 전 조회 2,603
12년 전 조회 3,316
12년 전 조회 2,592
12년 전 조회 2,377
12년 전 조회 2,915
12년 전 조회 1.6만
12년 전 조회 2,190
12년 전 조회 1,996
12년 전 조회 3,788
12년 전 조회 1만
12년 전 조회 2,357
12년 전 조회 2,300
12년 전 조회 2,746
12년 전 조회 2,215
12년 전 조회 2,363
12년 전 조회 4,638
12년 전 조회 2,589
12년 전 조회 3,246
12년 전 조회 2,242
12년 전 조회 2,408
12년 전 조회 4,130
12년 전 조회 1,971
12년 전 조회 2,468
12년 전 조회 2,785
12년 전 조회 3,223
12년 전 조회 3,333
12년 전 조회 3,809
12년 전 조회 2,237
13년 전 조회 2,262
13년 전 조회 5,642
13년 전 조회 2,708
13년 전 조회 2,872
13년 전 조회 6,729
13년 전 조회 4,446
13년 전 조회 3,368
13년 전 조회 2,554
13년 전 조회 4,719
13년 전 조회 3,710
13년 전 조회 3,414
13년 전 조회 3,060
13년 전 조회 2,758
13년 전 조회 2,740
13년 전 조회 2,481
13년 전 조회 3,609
13년 전 조회 2,906
13년 전 조회 2,564
13년 전 조회 3,619
13년 전 조회 2,831
13년 전 조회 3,435
13년 전 조회 4,668
13년 전 조회 2,432
13년 전 조회 3,419
13년 전 조회 2,247
13년 전 조회 2,711
13년 전 조회 3,344
13년 전 조회 2,983
13년 전 조회 5,774
13년 전 조회 3,906
13년 전 조회 4,088
13년 전 조회 2,438
13년 전 조회 9,755
13년 전 조회 4,609
13년 전 조회 2,661
13년 전 조회 2,532
13년 전 조회 3,911
13년 전 조회 6,642
13년 전 조회 4,647