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

· 12년 전 · 1708

<?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,954
12년 전 조회 8,840
12년 전 조회 4,416
12년 전 조회 4,013
12년 전 조회 1,622
12년 전 조회 1,675
12년 전 조회 3,405
12년 전 조회 4,242
12년 전 조회 4,641
12년 전 조회 1,869
12년 전 조회 1,546
12년 전 조회 1,139
12년 전 조회 1,232
12년 전 조회 2,975
12년 전 조회 2,993
12년 전 조회 1,513
12년 전 조회 3,057
12년 전 조회 1,528
12년 전 조회 8,713
12년 전 조회 1,784
12년 전 조회 4,023
12년 전 조회 6,657
12년 전 조회 1,826
12년 전 조회 3,099
12년 전 조회 852
12년 전 조회 1,545
12년 전 조회 1,400
12년 전 조회 1,555
12년 전 조회 3,560
12년 전 조회 1,342
12년 전 조회 2,073
12년 전 조회 1,509
12년 전 조회 1,165
12년 전 조회 1,641
12년 전 조회 2,378
12년 전 조회 1,630
12년 전 조회 1,538
12년 전 조회 2,053
12년 전 조회 1.5만
12년 전 조회 1,313
12년 전 조회 1,127
12년 전 조회 2,974
12년 전 조회 9,199
12년 전 조회 1,511
12년 전 조회 1,435
12년 전 조회 1,888
12년 전 조회 1,386
12년 전 조회 1,533
12년 전 조회 3,813
12년 전 조회 1,709
12년 전 조회 2,381
12년 전 조회 1,419
12년 전 조회 1,566
12년 전 조회 3,274
12년 전 조회 1,129
12년 전 조회 1,591
12년 전 조회 1,856
12년 전 조회 2,282
12년 전 조회 2,402
12년 전 조회 2,821
12년 전 조회 1,291
12년 전 조회 1,311
12년 전 조회 4,712
12년 전 조회 1,750
12년 전 조회 1,892
12년 전 조회 5,756
12년 전 조회 3,491
12년 전 조회 2,398
12년 전 조회 1,550
12년 전 조회 3,737
12년 전 조회 2,760
12년 전 조회 2,466
12년 전 조회 2,102
12년 전 조회 1,784
12년 전 조회 1,786
12년 전 조회 1,533
12년 전 조회 2,661
12년 전 조회 1,933
12년 전 조회 1,613
12년 전 조회 2,698
12년 전 조회 1,870
12년 전 조회 2,487
12년 전 조회 3,703
12년 전 조회 1,496
12년 전 조회 2,451
12년 전 조회 1,309
12년 전 조회 1,801
12년 전 조회 2,374
12년 전 조회 2,005
12년 전 조회 4,816
12년 전 조회 2,919
12년 전 조회 3,098
12년 전 조회 1,462
12년 전 조회 8,782
12년 전 조회 3,609
12년 전 조회 1,682
12년 전 조회 1,547
12년 전 조회 2,940
12년 전 조회 5,619
12년 전 조회 3,662