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

· 12년 전 · 2145

<?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,480
12년 전 조회 9,374
12년 전 조회 4,919
12년 전 조회 4,548
12년 전 조회 2,134
12년 전 조회 2,176
12년 전 조회 3,937
12년 전 조회 4,768
12년 전 조회 5,155
12년 전 조회 2,398
12년 전 조회 2,069
12년 전 조회 1,649
12년 전 조회 1,712
12년 전 조회 3,522
12년 전 조회 3,500
12년 전 조회 2,032
12년 전 조회 3,556
12년 전 조회 2,046
12년 전 조회 9,311
12년 전 조회 2,294
12년 전 조회 4,520
12년 전 조회 7,177
12년 전 조회 2,330
12년 전 조회 3,592
12년 전 조회 1,332
12년 전 조회 2,031
12년 전 조회 1,912
12년 전 조회 2,085
12년 전 조회 4,069
12년 전 조회 1,840
12년 전 조회 2,585
12년 전 조회 2,038
12년 전 조회 1,665
12년 전 조회 2,094
12년 전 조회 2,839
12년 전 조회 2,125
12년 전 조회 1,964
12년 전 조회 2,481
12년 전 조회 1.5만
12년 전 조회 1,746
12년 전 조회 1,522
12년 전 조회 3,375
12년 전 조회 9,606
12년 전 조회 1,915
12년 전 조회 1,871
12년 전 조회 2,307
12년 전 조회 1,795
12년 전 조회 1,974
12년 전 조회 4,237
12년 전 조회 2,146
12년 전 조회 2,809
12년 전 조회 1,804
12년 전 조회 1,977
12년 전 조회 3,681
12년 전 조회 1,523
12년 전 조회 2,017
12년 전 조회 2,341
12년 전 조회 2,769
12년 전 조회 2,876
12년 전 조회 3,319
12년 전 조회 1,778
12년 전 조회 1,831
12년 전 조회 5,197
12년 전 조회 2,249
12년 전 조회 2,434
12년 전 조회 6,261
12년 전 조회 3,951
12년 전 조회 2,927
12년 전 조회 2,067
12년 전 조회 4,243
12년 전 조회 3,254
12년 전 조회 2,929
12년 전 조회 2,607
12년 전 조회 2,289
12년 전 조회 2,307
12년 전 조회 2,042
12년 전 조회 3,118
12년 전 조회 2,438
12년 전 조회 2,141
12년 전 조회 3,205
12년 전 조회 2,390
12년 전 조회 2,975
12년 전 조회 4,200
12년 전 조회 1,952
12년 전 조회 2,932
12년 전 조회 1,792
12년 전 조회 2,275
12년 전 조회 2,915
12년 전 조회 2,517
12년 전 조회 5,328
12년 전 조회 3,443
12년 전 조회 3,639
12년 전 조회 2,000
12년 전 조회 9,327
12년 전 조회 4,140
12년 전 조회 2,202
12년 전 조회 2,068
12년 전 조회 3,496
12년 전 조회 6,183
12년 전 조회 4,197