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

· 12년 전 · 1452

<?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,687
12년 전 조회 8,572
12년 전 조회 4,169
12년 전 조회 3,735
12년 전 조회 1,352
12년 전 조회 1,418
12년 전 조회 3,142
12년 전 조회 3,978
12년 전 조회 4,384
12년 전 조회 1,599
12년 전 조회 1,288
12년 전 조회 874
12년 전 조회 972
12년 전 조회 2,713
12년 전 조회 2,724
12년 전 조회 1,248
12년 전 조회 2,792
12년 전 조회 1,269
12년 전 조회 8,453
12년 전 조회 1,506
12년 전 조회 3,766
12년 전 조회 6,377
12년 전 조회 1,559
12년 전 조회 2,828
12년 전 조회 579
12년 전 조회 1,285
12년 전 조회 1,148
12년 전 조회 1,306
12년 전 조회 3,300
12년 전 조회 1,074
12년 전 조회 1,810
12년 전 조회 1,248
12년 전 조회 898
12년 전 조회 1,394
12년 전 조회 2,096
12년 전 조회 1,377
12년 전 조회 1,259
12년 전 조회 1,784
12년 전 조회 1.5만
12년 전 조회 1,056
12년 전 조회 857
12년 전 조회 2,702
12년 전 조회 8,952
12년 전 조회 1,274
12년 전 조회 1,167
12년 전 조회 1,624
12년 전 조회 1,128
12년 전 조회 1,260
12년 전 조회 3,545
12년 전 조회 1,453
12년 전 조회 2,120
12년 전 조회 1,147
12년 전 조회 1,312
12년 전 조회 3,020
12년 전 조회 868
12년 전 조회 1,334
12년 전 조회 1,600
12년 전 조회 2,026
12년 전 조회 2,129
12년 전 조회 2,556
12년 전 조회 1,052
12년 전 조회 1,072
12년 전 조회 4,436
12년 전 조회 1,488
12년 전 조회 1,629
12년 전 조회 5,487
12년 전 조회 3,263
12년 전 조회 2,133
12년 전 조회 1,293
12년 전 조회 3,482
12년 전 조회 2,511
12년 전 조회 2,221
12년 전 조회 1,836
12년 전 조회 1,532
12년 전 조회 1,524
12년 전 조회 1,278
12년 전 조회 2,396
12년 전 조회 1,667
12년 전 조회 1,343
12년 전 조회 2,429
12년 전 조회 1,605
12년 전 조회 2,218
12년 전 조회 3,438
12년 전 조회 1,223
12년 전 조회 2,200
12년 전 조회 1,035
12년 전 조회 1,520
12년 전 조회 2,111
12년 전 조회 1,741
12년 전 조회 4,557
12년 전 조회 2,655
12년 전 조회 2,822
12년 전 조회 1,226
12년 전 조회 8,536
12년 전 조회 3,332
12년 전 조회 1,431
12년 전 조회 1,280
12년 전 조회 2,679
12년 전 조회 5,358
12년 전 조회 3,409
🐛 버그신고