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

· 13년 전 · 2898

<?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,222
12년 전 조회 1만
12년 전 조회 5,676
12년 전 조회 5,280
12년 전 조회 2,865
12년 전 조회 2,944
12년 전 조회 4,708
12년 전 조회 5,524
12년 전 조회 5,932
12년 전 조회 3,124
12년 전 조회 2,827
12년 전 조회 2,433
12년 전 조회 2,484
12년 전 조회 4,257
12년 전 조회 4,255
12년 전 조회 2,792
12년 전 조회 4,338
12년 전 조회 2,792
12년 전 조회 9,818
12년 전 조회 3,095
12년 전 조회 5,294
12년 전 조회 7,936
12년 전 조회 3,113
12년 전 조회 4,386
12년 전 조회 2,114
12년 전 조회 2,786
12년 전 조회 2,655
12년 전 조회 2,851
12년 전 조회 4,836
12년 전 조회 2,627
12년 전 조회 3,331
12년 전 조회 2,758
12년 전 조회 2,456
12년 전 조회 2,889
13년 전 조회 3,642
13년 전 조회 2,847
13년 전 조회 2,673
13년 전 조회 3,229
13년 전 조회 1.6만
13년 전 조회 2,488
13년 전 조회 2,280
13년 전 조회 4,105
13년 전 조회 1만
13년 전 조회 2,661
13년 전 조회 2,619
13년 전 조회 3,053
13년 전 조회 2,515
13년 전 조회 2,670
13년 전 조회 4,947
13년 전 조회 2,899
13년 전 조회 3,553
13년 전 조회 2,533
13년 전 조회 2,720
13년 전 조회 4,448
13년 전 조회 2,287
13년 전 조회 2,786
13년 전 조회 3,087
13년 전 조회 3,530
13년 전 조회 3,631
13년 전 조회 4,082
13년 전 조회 2,543
13년 전 조회 2,555
13년 전 조회 5,944
13년 전 조회 3,006
13년 전 조회 3,166
13년 전 조회 7,040
13년 전 조회 4,713
13년 전 조회 3,665
13년 전 조회 2,842
13년 전 조회 5,018
13년 전 조회 4,032
13년 전 조회 3,701
13년 전 조회 3,394
13년 전 조회 3,063
13년 전 조회 3,049
13년 전 조회 2,807
13년 전 조회 3,928
13년 전 조회 3,237
13년 전 조회 2,858
13년 전 조회 3,946
13년 전 조회 3,158
13년 전 조회 3,755
13년 전 조회 4,966
13년 전 조회 2,737
13년 전 조회 3,696
13년 전 조회 2,532
13년 전 조회 3,025
13년 전 조회 3,636
13년 전 조회 3,288
13년 전 조회 6,100
13년 전 조회 4,208
13년 전 조회 4,387
13년 전 조회 2,765
13년 전 조회 1만
13년 전 조회 4,900
13년 전 조회 2,973
13년 전 조회 2,847
13년 전 조회 4,238
13년 전 조회 6,974
13년 전 조회 4,956