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

· 12년 전 · 1465

<?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,184
12년 전 조회 3,750
12년 전 조회 1,365
12년 전 조회 1,438
12년 전 조회 3,153
12년 전 조회 3,992
12년 전 조회 4,397
12년 전 조회 1,611
12년 전 조회 1,301
12년 전 조회 887
12년 전 조회 988
12년 전 조회 2,725
12년 전 조회 2,740
12년 전 조회 1,260
12년 전 조회 2,806
12년 전 조회 1,283
12년 전 조회 8,467
12년 전 조회 1,520
12년 전 조회 3,776
12년 전 조회 6,390
12년 전 조회 1,572
12년 전 조회 2,841
12년 전 조회 592
12년 전 조회 1,302
12년 전 조회 1,162
12년 전 조회 1,320
12년 전 조회 3,315
12년 전 조회 1,089
12년 전 조회 1,824
12년 전 조회 1,263
12년 전 조회 914
12년 전 조회 1,402
12년 전 조회 2,112
12년 전 조회 1,394
12년 전 조회 1,275
12년 전 조회 1,797
12년 전 조회 1.5만
12년 전 조회 1,071
12년 전 조회 874
12년 전 조회 2,721
12년 전 조회 8,965
12년 전 조회 1,287
12년 전 조회 1,180
12년 전 조회 1,640
12년 전 조회 1,141
12년 전 조회 1,274
12년 전 조회 3,559
12년 전 조회 1,466
12년 전 조회 2,136
12년 전 조회 1,161
12년 전 조회 1,322
12년 전 조회 3,034
12년 전 조회 882
12년 전 조회 1,349
12년 전 조회 1,616
12년 전 조회 2,044
12년 전 조회 2,143
12년 전 조회 2,570
12년 전 조회 1,060
12년 전 조회 1,085
12년 전 조회 4,450
12년 전 조회 1,503
12년 전 조회 1,642
12년 전 조회 5,503
12년 전 조회 3,271
12년 전 조회 2,148
12년 전 조회 1,307
12년 전 조회 3,495
12년 전 조회 2,528
12년 전 조회 2,234
12년 전 조회 1,853
12년 전 조회 1,549
12년 전 조회 1,538
12년 전 조회 1,294
12년 전 조회 2,410
12년 전 조회 1,685
12년 전 조회 1,360
12년 전 조회 2,445
12년 전 조회 1,621
12년 전 조회 2,235
12년 전 조회 3,456
12년 전 조회 1,241
12년 전 조회 2,209
12년 전 조회 1,049
12년 전 조회 1,535
12년 전 조회 2,127
12년 전 조회 1,761
12년 전 조회 4,571
12년 전 조회 2,671
12년 전 조회 2,840
12년 전 조회 1,232
12년 전 조회 8,552
12년 전 조회 3,345
12년 전 조회 1,445
12년 전 조회 1,296
12년 전 조회 2,694
12년 전 조회 5,370
12년 전 조회 3,423
🐛 버그신고