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

· 13년 전 · 2902

<?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년 전 조회 5,225
12년 전 조회 1만
12년 전 조회 5,680
12년 전 조회 5,284
12년 전 조회 2,869
12년 전 조회 2,949
12년 전 조회 4,712
12년 전 조회 5,525
12년 전 조회 5,936
12년 전 조회 3,130
12년 전 조회 2,829
12년 전 조회 2,437
12년 전 조회 2,487
12년 전 조회 4,259
12년 전 조회 4,257
12년 전 조회 2,793
12년 전 조회 4,342
12년 전 조회 2,793
12년 전 조회 9,818
12년 전 조회 3,100
12년 전 조회 5,297
12년 전 조회 7,937
12년 전 조회 3,118
12년 전 조회 4,390
12년 전 조회 2,118
12년 전 조회 2,788
12년 전 조회 2,658
12년 전 조회 2,854
12년 전 조회 4,838
12년 전 조회 2,629
12년 전 조회 3,334
12년 전 조회 2,760
12년 전 조회 2,459
12년 전 조회 2,889
13년 전 조회 3,644
13년 전 조회 2,851
13년 전 조회 2,673
13년 전 조회 3,234
13년 전 조회 1.6만
13년 전 조회 2,492
13년 전 조회 2,283
13년 전 조회 4,106
13년 전 조회 1만
13년 전 조회 2,662
13년 전 조회 2,624
13년 전 조회 3,055
13년 전 조회 2,517
13년 전 조회 2,675
13년 전 조회 4,950
13년 전 조회 2,903
13년 전 조회 3,553
13년 전 조회 2,540
13년 전 조회 2,725
13년 전 조회 4,451
13년 전 조회 2,295
13년 전 조회 2,789
13년 전 조회 3,089
13년 전 조회 3,531
13년 전 조회 3,631
13년 전 조회 4,083
13년 전 조회 2,547
13년 전 조회 2,557
13년 전 조회 5,952
13년 전 조회 3,011
13년 전 조회 3,172
13년 전 조회 7,044
13년 전 조회 4,717
13년 전 조회 3,666
13년 전 조회 2,845
13년 전 조회 5,019
13년 전 조회 4,033
13년 전 조회 3,705
13년 전 조회 3,399
13년 전 조회 3,067
13년 전 조회 3,052
13년 전 조회 2,811
13년 전 조회 3,934
13년 전 조회 3,242
13년 전 조회 2,858
13년 전 조회 3,953
13년 전 조회 3,162
13년 전 조회 3,761
13년 전 조회 4,967
13년 전 조회 2,745
13년 전 조회 3,698
13년 전 조회 2,534
13년 전 조회 3,028
13년 전 조회 3,644
13년 전 조회 3,290
13년 전 조회 6,104
13년 전 조회 4,215
13년 전 조회 4,388
13년 전 조회 2,768
13년 전 조회 1만
13년 전 조회 4,906
13년 전 조회 2,974
13년 전 조회 2,851
13년 전 조회 4,242
13년 전 조회 6,976
13년 전 조회 4,958