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

· 12년 전 · 1466

<?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년 전 조회 3,705
12년 전 조회 8,590
12년 전 조회 4,188
12년 전 조회 3,752
12년 전 조회 1,370
12년 전 조회 1,440
12년 전 조회 3,157
12년 전 조회 3,996
12년 전 조회 4,400
12년 전 조회 1,614
12년 전 조회 1,303
12년 전 조회 891
12년 전 조회 991
12년 전 조회 2,729
12년 전 조회 2,742
12년 전 조회 1,264
12년 전 조회 2,810
12년 전 조회 1,285
12년 전 조회 8,471
12년 전 조회 1,522
12년 전 조회 3,779
12년 전 조회 6,391
12년 전 조회 1,574
12년 전 조회 2,842
12년 전 조회 593
12년 전 조회 1,305
12년 전 조회 1,164
12년 전 조회 1,324
12년 전 조회 3,317
12년 전 조회 1,090
12년 전 조회 1,825
12년 전 조회 1,264
12년 전 조회 916
12년 전 조회 1,403
12년 전 조회 2,114
12년 전 조회 1,394
12년 전 조회 1,278
12년 전 조회 1,798
12년 전 조회 1.5만
12년 전 조회 1,073
12년 전 조회 875
12년 전 조회 2,722
12년 전 조회 8,967
12년 전 조회 1,287
12년 전 조회 1,181
12년 전 조회 1,642
12년 전 조회 1,146
12년 전 조회 1,275
12년 전 조회 3,561
12년 전 조회 1,467
12년 전 조회 2,138
12년 전 조회 1,165
12년 전 조회 1,323
12년 전 조회 3,038
12년 전 조회 884
12년 전 조회 1,350
12년 전 조회 1,616
12년 전 조회 2,044
12년 전 조회 2,144
12년 전 조회 2,574
12년 전 조회 1,061
12년 전 조회 1,090
12년 전 조회 4,452
12년 전 조회 1,504
12년 전 조회 1,644
12년 전 조회 5,505
12년 전 조회 3,272
12년 전 조회 2,148
12년 전 조회 1,307
12년 전 조회 3,498
12년 전 조회 2,530
12년 전 조회 2,236
12년 전 조회 1,856
12년 전 조회 1,550
12년 전 조회 1,541
12년 전 조회 1,296
12년 전 조회 2,413
12년 전 조회 1,687
12년 전 조회 1,362
12년 전 조회 2,447
12년 전 조회 1,624
12년 전 조회 2,238
12년 전 조회 3,458
12년 전 조회 1,243
12년 전 조회 2,212
12년 전 조회 1,051
12년 전 조회 1,540
12년 전 조회 2,129
12년 전 조회 1,764
12년 전 조회 4,574
12년 전 조회 2,673
12년 전 조회 2,841
12년 전 조회 1,233
12년 전 조회 8,553
12년 전 조회 3,346
12년 전 조회 1,445
12년 전 조회 1,299
12년 전 조회 2,695
12년 전 조회 5,372
12년 전 조회 3,425
🐛 버그신고