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

· 12년 전 · 2492

<?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,836
12년 전 조회 9,705
12년 전 조회 5,304
12년 전 조회 4,886
12년 전 조회 2,488
12년 전 조회 2,560
12년 전 조회 4,290
12년 전 조회 5,135
12년 전 조회 5,525
12년 전 조회 2,750
12년 전 조회 2,446
12년 전 조회 2,042
12년 전 조회 2,115
12년 전 조회 3,895
12년 전 조회 3,873
12년 전 조회 2,424
12년 전 조회 3,939
12년 전 조회 2,448
12년 전 조회 9,606
12년 전 조회 2,702
12년 전 조회 4,933
12년 전 조회 7,528
12년 전 조회 2,708
12년 전 조회 4,011
12년 전 조회 1,715
12년 전 조회 2,399
12년 전 조회 2,279
12년 전 조회 2,449
12년 전 조회 4,466
12년 전 조회 2,236
12년 전 조회 2,977
12년 전 조회 2,387
12년 전 조회 2,060
12년 전 조회 2,500
12년 전 조회 3,214
12년 전 조회 2,489
12년 전 조회 2,296
12년 전 조회 2,818
12년 전 조회 1.6만
12년 전 조회 2,098
12년 전 조회 1,900
12년 전 조회 3,712
12년 전 조회 9,964
12년 전 조회 2,264
12년 전 조회 2,217
12년 전 조회 2,665
12년 전 조회 2,130
12년 전 조회 2,281
12년 전 조회 4,552
12년 전 조회 2,493
12년 전 조회 3,159
12년 전 조회 2,143
12년 전 조회 2,320
12년 전 조회 4,036
12년 전 조회 1,883
12년 전 조회 2,369
12년 전 조회 2,697
12년 전 조회 3,135
12년 전 조회 3,234
12년 전 조회 3,704
12년 전 조회 2,149
12년 전 조회 2,187
12년 전 조회 5,558
12년 전 조회 2,611
12년 전 조회 2,796
12년 전 조회 6,625
12년 전 조회 4,353
12년 전 조회 3,285
12년 전 조회 2,456
12년 전 조회 4,636
12년 전 조회 3,622
13년 전 조회 3,325
13년 전 조회 2,977
13년 전 조회 2,657
13년 전 조회 2,679
13년 전 조회 2,406
13년 전 조회 3,499
13년 전 조회 2,822
13년 전 조회 2,484
13년 전 조회 3,537
13년 전 조회 2,756
13년 전 조회 3,342
13년 전 조회 4,575
13년 전 조회 2,336
13년 전 조회 3,335
13년 전 조회 2,163
13년 전 조회 2,626
13년 전 조회 3,266
13년 전 조회 2,901
13년 전 조회 5,689
13년 전 조회 3,802
13년 전 조회 4,010
13년 전 조회 2,357
13년 전 조회 9,671
13년 전 조회 4,510
13년 전 조회 2,565
13년 전 조회 2,437
13년 전 조회 3,832
13년 전 조회 6,550
13년 전 조회 4,557