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

· 12년 전 · 2085

<?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년 전 조회 4,385
12년 전 조회 9,282
12년 전 조회 4,835
12년 전 조회 4,458
12년 전 조회 2,051
12년 전 조회 2,097
12년 전 조회 3,844
12년 전 조회 4,677
12년 전 조회 5,078
12년 전 조회 2,308
12년 전 조회 1,982
12년 전 조회 1,551
12년 전 조회 1,633
12년 전 조회 3,425
12년 전 조회 3,410
12년 전 조회 1,942
12년 전 조회 3,476
12년 전 조회 1,946
12년 전 조회 9,252
12년 전 조회 2,210
12년 전 조회 4,428
12년 전 조회 7,085
12년 전 조회 2,240
12년 전 조회 3,524
12년 전 조회 1,241
12년 전 조회 1,952
12년 전 조회 1,834
12년 전 조회 2,006
12년 전 조회 3,989
12년 전 조회 1,765
12년 전 조회 2,498
12년 전 조회 1,951
12년 전 조회 1,584
12년 전 조회 2,014
12년 전 조회 2,753
12년 전 조회 2,059
12년 전 조회 1,904
12년 전 조회 2,419
12년 전 조회 1.5만
12년 전 조회 1,681
12년 전 조회 1,460
12년 전 조회 3,317
12년 전 조회 9,545
12년 전 조회 1,857
12년 전 조회 1,808
12년 전 조회 2,254
12년 전 조회 1,728
12년 전 조회 1,906
12년 전 조회 4,183
12년 전 조회 2,086
12년 전 조회 2,752
12년 전 조회 1,745
12년 전 조회 1,922
12년 전 조회 3,626
12년 전 조회 1,464
12년 전 조회 1,945
12년 전 조회 2,256
12년 전 조회 2,685
12년 전 조회 2,791
12년 전 조회 3,229
12년 전 조회 1,684
12년 전 조회 1,727
12년 전 조회 5,102
12년 전 조회 2,159
12년 전 조회 2,334
12년 전 조회 6,159
12년 전 조회 3,864
12년 전 조회 2,831
12년 전 조회 1,983
12년 전 조회 4,148
12년 전 조회 3,182
12년 전 조회 2,838
12년 전 조회 2,522
12년 전 조회 2,213
12년 전 조회 2,223
12년 전 조회 1,960
12년 전 조회 3,042
12년 전 조회 2,358
12년 전 조회 2,056
12년 전 조회 3,132
12년 전 조회 2,319
12년 전 조회 2,898
12년 전 조회 4,119
12년 전 조회 1,882
12년 전 조회 2,853
12년 전 조회 1,708
12년 전 조회 2,195
12년 전 조회 2,827
12년 전 조회 2,438
12년 전 조회 5,236
12년 전 조회 3,359
12년 전 조회 3,545
12년 전 조회 1,900
12년 전 조회 9,227
12년 전 조회 4,039
12년 전 조회 2,105
12년 전 조회 1,960
12년 전 조회 3,399
12년 전 조회 6,081
12년 전 조회 4,098