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

· 12년 전 · 1457

<?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년 전 조회 3,691
12년 전 조회 8,578
12년 전 조회 4,172
12년 전 조회 3,740
12년 전 조회 1,358
12년 전 조회 1,426
12년 전 조회 3,146
12년 전 조회 3,982
12년 전 조회 4,390
12년 전 조회 1,603
12년 전 조회 1,293
12년 전 조회 877
12년 전 조회 978
12년 전 조회 2,717
12년 전 조회 2,727
12년 전 조회 1,250
12년 전 조회 2,797
12년 전 조회 1,273
12년 전 조회 8,456
12년 전 조회 1,512
12년 전 조회 3,767
12년 전 조회 6,381
12년 전 조회 1,563
12년 전 조회 2,832
12년 전 조회 582
12년 전 조회 1,290
12년 전 조회 1,149
12년 전 조회 1,311
12년 전 조회 3,302
12년 전 조회 1,083
12년 전 조회 1,814
12년 전 조회 1,253
12년 전 조회 905
12년 전 조회 1,396
12년 전 조회 2,101
12년 전 조회 1,382
12년 전 조회 1,265
12년 전 조회 1,790
12년 전 조회 1.5만
12년 전 조회 1,060
12년 전 조회 866
12년 전 조회 2,708
12년 전 조회 8,955
12년 전 조회 1,275
12년 전 조회 1,171
12년 전 조회 1,630
12년 전 조회 1,131
12년 전 조회 1,266
12년 전 조회 3,549
12년 전 조회 1,458
12년 전 조회 2,126
12년 전 조회 1,152
12년 전 조회 1,313
12년 전 조회 3,023
12년 전 조회 872
12년 전 조회 1,338
12년 전 조회 1,606
12년 전 조회 2,032
12년 전 조회 2,134
12년 전 조회 2,560
12년 전 조회 1,054
12년 전 조회 1,076
12년 전 조회 4,440
12년 전 조회 1,495
12년 전 조회 1,632
12년 전 조회 5,493
12년 전 조회 3,265
12년 전 조회 2,136
12년 전 조회 1,298
12년 전 조회 3,486
12년 전 조회 2,515
12년 전 조회 2,223
12년 전 조회 1,844
12년 전 조회 1,539
12년 전 조회 1,527
12년 전 조회 1,281
12년 전 조회 2,401
12년 전 조회 1,669
12년 전 조회 1,348
12년 전 조회 2,434
12년 전 조회 1,613
12년 전 조회 2,226
12년 전 조회 3,443
12년 전 조회 1,229
12년 전 조회 2,202
12년 전 조회 1,038
12년 전 조회 1,526
12년 전 조회 2,117
12년 전 조회 1,748
12년 전 조회 4,564
12년 전 조회 2,663
12년 전 조회 2,828
12년 전 조회 1,227
12년 전 조회 8,538
12년 전 조회 3,335
12년 전 조회 1,436
12년 전 조회 1,285
12년 전 조회 2,683
12년 전 조회 5,361
12년 전 조회 3,411
🐛 버그신고