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

· 12년 전 · 2491

<?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,833
12년 전 조회 9,705
12년 전 조회 5,304
12년 전 조회 4,886
12년 전 조회 2,487
12년 전 조회 2,555
12년 전 조회 4,288
12년 전 조회 5,131
12년 전 조회 5,523
12년 전 조회 2,749
12년 전 조회 2,444
12년 전 조회 2,039
12년 전 조회 2,110
12년 전 조회 3,889
12년 전 조회 3,868
12년 전 조회 2,420
12년 전 조회 3,931
12년 전 조회 2,445
12년 전 조회 9,604
12년 전 조회 2,698
12년 전 조회 4,930
12년 전 조회 7,526
12년 전 조회 2,707
12년 전 조회 4,008
12년 전 조회 1,712
12년 전 조회 2,398
12년 전 조회 2,275
12년 전 조회 2,447
12년 전 조회 4,461
12년 전 조회 2,235
12년 전 조회 2,974
12년 전 조회 2,385
12년 전 조회 2,059
12년 전 조회 2,497
12년 전 조회 3,212
12년 전 조회 2,487
12년 전 조회 2,296
12년 전 조회 2,817
12년 전 조회 1.6만
12년 전 조회 2,094
12년 전 조회 1,897
12년 전 조회 3,710
12년 전 조회 9,963
12년 전 조회 2,261
12년 전 조회 2,217
12년 전 조회 2,664
12년 전 조회 2,129
12년 전 조회 2,279
12년 전 조회 4,549
12년 전 조회 2,492
12년 전 조회 3,159
12년 전 조회 2,138
12년 전 조회 2,319
12년 전 조회 4,034
12년 전 조회 1,881
12년 전 조회 2,365
12년 전 조회 2,695
12년 전 조회 3,133
12년 전 조회 3,233
12년 전 조회 3,697
12년 전 조회 2,147
12년 전 조회 2,186
12년 전 조회 5,556
12년 전 조회 2,610
12년 전 조회 2,794
12년 전 조회 6,623
12년 전 조회 4,351
12년 전 조회 3,281
12년 전 조회 2,454
12년 전 조회 4,636
12년 전 조회 3,619
13년 전 조회 3,324
13년 전 조회 2,975
13년 전 조회 2,657
13년 전 조회 2,676
13년 전 조회 2,404
13년 전 조회 3,497
13년 전 조회 2,821
13년 전 조회 2,481
13년 전 조회 3,534
13년 전 조회 2,754
13년 전 조회 3,334
13년 전 조회 4,574
13년 전 조회 2,335
13년 전 조회 3,334
13년 전 조회 2,161
13년 전 조회 2,624
13년 전 조회 3,265
13년 전 조회 2,901
13년 전 조회 5,686
13년 전 조회 3,799
13년 전 조회 4,009
13년 전 조회 2,357
13년 전 조회 9,669
13년 전 조회 4,505
13년 전 조회 2,561
13년 전 조회 2,431
13년 전 조회 3,832
13년 전 조회 6,549
13년 전 조회 4,555