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

· 12년 전 · 1464

<?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,703
12년 전 조회 8,588
12년 전 조회 4,182
12년 전 조회 3,750
12년 전 조회 1,365
12년 전 조회 1,438
12년 전 조회 3,153
12년 전 조회 3,992
12년 전 조회 4,397
12년 전 조회 1,610
12년 전 조회 1,301
12년 전 조회 886
12년 전 조회 988
12년 전 조회 2,725
12년 전 조회 2,737
12년 전 조회 1,259
12년 전 조회 2,806
12년 전 조회 1,283
12년 전 조회 8,466
12년 전 조회 1,520
12년 전 조회 3,776
12년 전 조회 6,388
12년 전 조회 1,571
12년 전 조회 2,840
12년 전 조회 592
12년 전 조회 1,301
12년 전 조회 1,162
12년 전 조회 1,318
12년 전 조회 3,315
12년 전 조회 1,089
12년 전 조회 1,823
12년 전 조회 1,261
12년 전 조회 913
12년 전 조회 1,402
12년 전 조회 2,110
12년 전 조회 1,394
12년 전 조회 1,274
12년 전 조회 1,797
12년 전 조회 1.5만
12년 전 조회 1,071
12년 전 조회 874
12년 전 조회 2,719
12년 전 조회 8,964
12년 전 조회 1,283
12년 전 조회 1,179
12년 전 조회 1,640
12년 전 조회 1,139
12년 전 조회 1,274
12년 전 조회 3,559
12년 전 조회 1,465
12년 전 조회 2,136
12년 전 조회 1,159
12년 전 조회 1,321
12년 전 조회 3,034
12년 전 조회 882
12년 전 조회 1,348
12년 전 조회 1,615
12년 전 조회 2,044
12년 전 조회 2,142
12년 전 조회 2,570
12년 전 조회 1,059
12년 전 조회 1,083
12년 전 조회 4,450
12년 전 조회 1,503
12년 전 조회 1,642
12년 전 조회 5,502
12년 전 조회 3,271
12년 전 조회 2,148
12년 전 조회 1,306
12년 전 조회 3,493
12년 전 조회 2,527
12년 전 조회 2,231
12년 전 조회 1,852
12년 전 조회 1,548
12년 전 조회 1,537
12년 전 조회 1,293
12년 전 조회 2,407
12년 전 조회 1,684
12년 전 조회 1,357
12년 전 조회 2,445
12년 전 조회 1,621
12년 전 조회 2,234
12년 전 조회 3,456
12년 전 조회 1,241
12년 전 조회 2,208
12년 전 조회 1,049
12년 전 조회 1,534
12년 전 조회 2,127
12년 전 조회 1,761
12년 전 조회 4,571
12년 전 조회 2,671
12년 전 조회 2,840
12년 전 조회 1,232
12년 전 조회 8,550
12년 전 조회 3,343
12년 전 조회 1,444
12년 전 조회 1,296
12년 전 조회 2,693
12년 전 조회 5,370
12년 전 조회 3,421
🐛 버그신고