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

· 13년 전 · 2819

<?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,164
12년 전 조회 1만
12년 전 조회 5,627
12년 전 조회 5,228
12년 전 조회 2,808
12년 전 조회 2,888
12년 전 조회 4,640
12년 전 조회 5,455
12년 전 조회 5,859
12년 전 조회 3,063
12년 전 조회 2,763
12년 전 조회 2,394
12년 전 조회 2,435
12년 전 조회 4,196
12년 전 조회 4,196
12년 전 조회 2,735
12년 전 조회 4,288
12년 전 조회 2,736
12년 전 조회 9,782
12년 전 조회 3,032
12년 전 조회 5,238
12년 전 조회 7,875
12년 전 조회 3,038
12년 전 조회 4,335
12년 전 조회 2,052
12년 전 조회 2,732
12년 전 조회 2,590
12년 전 조회 2,773
12년 전 조회 4,770
12년 전 조회 2,562
12년 전 조회 3,267
12년 전 조회 2,697
12년 전 조회 2,380
12년 전 조회 2,840
12년 전 조회 3,578
12년 전 조회 2,793
12년 전 조회 2,601
12년 전 조회 3,156
12년 전 조회 1.6만
12년 전 조회 2,418
13년 전 조회 2,222
13년 전 조회 4,027
13년 전 조회 1만
13년 전 조회 2,597
13년 전 조회 2,541
13년 전 조회 2,985
13년 전 조회 2,431
13년 전 조회 2,607
13년 전 조회 4,874
13년 전 조회 2,820
13년 전 조회 3,476
13년 전 조회 2,471
13년 전 조회 2,651
13년 전 조회 4,380
13년 전 조회 2,216
13년 전 조회 2,720
13년 전 조회 3,018
13년 전 조회 3,464
13년 전 조회 3,570
13년 전 조회 4,024
13년 전 조회 2,480
13년 전 조회 2,485
13년 전 조회 5,876
13년 전 조회 2,925
13년 전 조회 3,092
13년 전 조회 6,972
13년 전 조회 4,668
13년 전 조회 3,594
13년 전 조회 2,771
13년 전 조회 4,954
13년 전 조회 3,963
13년 전 조회 3,642
13년 전 조회 3,320
13년 전 조회 3,003
13년 전 조회 2,972
13년 전 조회 2,735
13년 전 조회 3,855
13년 전 조회 3,162
13년 전 조회 2,794
13년 전 조회 3,876
13년 전 조회 3,083
13년 전 조회 3,680
13년 전 조회 4,904
13년 전 조회 2,659
13년 전 조회 3,620
13년 전 조회 2,464
13년 전 조회 2,958
13년 전 조회 3,569
13년 전 조회 3,217
13년 전 조회 6,019
13년 전 조회 4,128
13년 전 조회 4,287
13년 전 조회 2,687
13년 전 조회 9,997
13년 전 조회 4,817
13년 전 조회 2,889
13년 전 조회 2,765
13년 전 조회 4,157
13년 전 조회 6,882
13년 전 조회 4,874