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

· 13년 전 · 2889

<?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년 전 조회 5,218
12년 전 조회 1만
12년 전 조회 5,672
12년 전 조회 5,275
12년 전 조회 2,859
12년 전 조회 2,940
12년 전 조회 4,705
12년 전 조회 5,514
12년 전 조회 5,925
12년 전 조회 3,118
12년 전 조회 2,823
12년 전 조회 2,430
12년 전 조회 2,480
12년 전 조회 4,249
12년 전 조회 4,255
12년 전 조회 2,786
12년 전 조회 4,333
12년 전 조회 2,788
12년 전 조회 9,814
12년 전 조회 3,087
12년 전 조회 5,288
12년 전 조회 7,933
12년 전 조회 3,110
12년 전 조회 4,382
12년 전 조회 2,109
12년 전 조회 2,784
12년 전 조회 2,647
12년 전 조회 2,847
12년 전 조회 4,827
12년 전 조회 2,620
12년 전 조회 3,326
12년 전 조회 2,753
12년 전 조회 2,449
12년 전 조회 2,883
13년 전 조회 3,639
13년 전 조회 2,840
13년 전 조회 2,666
13년 전 조회 3,221
13년 전 조회 1.6만
13년 전 조회 2,483
13년 전 조회 2,276
13년 전 조회 4,101
13년 전 조회 1만
13년 전 조회 2,659
13년 전 조회 2,611
13년 전 조회 3,051
13년 전 조회 2,510
13년 전 조회 2,663
13년 전 조회 4,940
13년 전 조회 2,890
13년 전 조회 3,544
13년 전 조회 2,528
13년 전 조회 2,714
13년 전 조회 4,443
13년 전 조회 2,281
13년 전 조회 2,778
13년 전 조회 3,086
13년 전 조회 3,522
13년 전 조회 3,625
13년 전 조회 4,080
13년 전 조회 2,536
13년 전 조회 2,551
13년 전 조회 5,938
13년 전 조회 2,994
13년 전 조회 3,160
13년 전 조회 7,035
13년 전 조회 4,708
13년 전 조회 3,658
13년 전 조회 2,838
13년 전 조회 5,013
13년 전 조회 4,027
13년 전 조회 3,699
13년 전 조회 3,386
13년 전 조회 3,054
13년 전 조회 3,037
13년 전 조회 2,801
13년 전 조회 3,922
13년 전 조회 3,232
13년 전 조회 2,850
13년 전 조회 3,942
13년 전 조회 3,147
13년 전 조회 3,746
13년 전 조회 4,962
13년 전 조회 2,731
13년 전 조회 3,689
13년 전 조회 2,528
13년 전 조회 3,021
13년 전 조회 3,627
13년 전 조회 3,281
13년 전 조회 6,093
13년 전 조회 4,197
13년 전 조회 4,379
13년 전 조회 2,759
13년 전 조회 1만
13년 전 조회 4,894
13년 전 조회 2,965
13년 전 조회 2,840
13년 전 조회 4,233
13년 전 조회 6,962
13년 전 조회 4,948