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

· 12년 전 · 1528

<?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,775
12년 전 조회 8,649
12년 전 조회 4,249
12년 전 조회 3,821
12년 전 조회 1,426
12년 전 조회 1,502
12년 전 조회 3,219
12년 전 조회 4,053
12년 전 조회 4,460
12년 전 조회 1,681
12년 전 조회 1,362
12년 전 조회 958
12년 전 조회 1,050
12년 전 조회 2,793
12년 전 조회 2,804
12년 전 조회 1,319
12년 전 조회 2,867
12년 전 조회 1,349
12년 전 조회 8,527
12년 전 조회 1,577
12년 전 조회 3,843
12년 전 조회 6,452
12년 전 조회 1,635
12년 전 조회 2,896
12년 전 조회 661
12년 전 조회 1,366
12년 전 조회 1,218
12년 전 조회 1,380
12년 전 조회 3,379
12년 전 조회 1,157
12년 전 조회 1,885
12년 전 조회 1,321
12년 전 조회 974
12년 전 조회 1,459
12년 전 조회 2,168
12년 전 조회 1,452
12년 전 조회 1,334
12년 전 조회 1,863
12년 전 조회 1.5만
12년 전 조회 1,131
12년 전 조회 950
12년 전 조회 2,785
12년 전 조회 9,027
12년 전 조회 1,343
12년 전 조회 1,236
12년 전 조회 1,696
12년 전 조회 1,215
12년 전 조회 1,353
12년 전 조회 3,622
12년 전 조회 1,529
12년 전 조회 2,197
12년 전 조회 1,217
12년 전 조회 1,366
12년 전 조회 3,093
12년 전 조회 947
12년 전 조회 1,414
12년 전 조회 1,677
12년 전 조회 2,099
12년 전 조회 2,197
12년 전 조회 2,636
12년 전 조회 1,117
12년 전 조회 1,144
12년 전 조회 4,518
12년 전 조회 1,557
12년 전 조회 1,705
12년 전 조회 5,569
12년 전 조회 3,328
12년 전 조회 2,205
12년 전 조회 1,368
12년 전 조회 3,552
12년 전 조회 2,584
12년 전 조회 2,284
12년 전 조회 1,903
12년 전 조회 1,606
12년 전 조회 1,597
12년 전 조회 1,350
12년 전 조회 2,463
12년 전 조회 1,735
12년 전 조회 1,422
12년 전 조회 2,494
12년 전 조회 1,677
12년 전 조회 2,294
12년 전 조회 3,516
12년 전 조회 1,302
12년 전 조회 2,270
12년 전 조회 1,110
12년 전 조회 1,591
12년 전 조회 2,181
12년 전 조회 1,822
12년 전 조회 4,626
12년 전 조회 2,727
12년 전 조회 2,902
12년 전 조회 1,290
12년 전 조회 8,597
12년 전 조회 3,396
12년 전 조회 1,496
12년 전 조회 1,345
12년 전 조회 2,749
12년 전 조회 5,426
12년 전 조회 3,473
🐛 버그신고