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

· 12년 전 · 2611

<?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,956
12년 전 조회 9,819
12년 전 조회 5,417
12년 전 조회 5,012
12년 전 조회 2,596
12년 전 조회 2,688
12년 전 조회 4,417
12년 전 조회 5,231
12년 전 조회 5,642
12년 전 조회 2,851
12년 전 조회 2,572
12년 전 조회 2,177
12년 전 조회 2,220
12년 전 조회 4,005
12년 전 조회 3,980
12년 전 조회 2,550
12년 전 조회 4,070
12년 전 조회 2,536
12년 전 조회 9,697
12년 전 조회 2,814
12년 전 조회 5,043
12년 전 조회 7,648
12년 전 조회 2,821
12년 전 조회 4,126
12년 전 조회 1,843
12년 전 조회 2,524
12년 전 조회 2,390
12년 전 조회 2,575
12년 전 조회 4,580
12년 전 조회 2,349
12년 전 조회 3,078
12년 전 조회 2,510
12년 전 조회 2,175
12년 전 조회 2,630
12년 전 조회 3,349
12년 전 조회 2,616
12년 전 조회 2,398
12년 전 조회 2,940
12년 전 조회 1.6만
12년 전 조회 2,216
12년 전 조회 2,035
12년 전 조회 3,819
12년 전 조회 1만
12년 전 조회 2,391
12년 전 조회 2,326
12년 전 조회 2,772
12년 전 조회 2,236
12년 전 조회 2,391
12년 전 조회 4,667
12년 전 조회 2,612
12년 전 조회 3,275
12년 전 조회 2,274
12년 전 조회 2,426
12년 전 조회 4,154
12년 전 조회 2,006
12년 전 조회 2,501
12년 전 조회 2,815
12년 전 조회 3,255
12년 전 조회 3,361
12년 전 조회 3,832
13년 전 조회 2,264
13년 전 조회 2,280
13년 전 조회 5,671
13년 전 조회 2,733
13년 전 조회 2,899
13년 전 조회 6,753
13년 전 조회 4,473
13년 전 조회 3,396
13년 전 조회 2,587
13년 전 조회 4,753
13년 전 조회 3,741
13년 전 조회 3,438
13년 전 조회 3,085
13년 전 조회 2,788
13년 전 조회 2,769
13년 전 조회 2,501
13년 전 조회 3,633
13년 전 조회 2,932
13년 전 조회 2,581
13년 전 조회 3,644
13년 전 조회 2,857
13년 전 조회 3,465
13년 전 조회 4,692
13년 전 조회 2,449
13년 전 조회 3,438
13년 전 조회 2,278
13년 전 조회 2,739
13년 전 조회 3,369
13년 전 조회 3,010
13년 전 조회 5,802
13년 전 조회 3,929
13년 전 조회 4,113
13년 전 조회 2,456
13년 전 조회 9,785
13년 전 조회 4,635
13년 전 조회 2,694
13년 전 조회 2,551
13년 전 조회 3,935
13년 전 조회 6,657
13년 전 조회 4,674