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

· 12년 전 · 1475

<?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,710
12년 전 조회 8,592
12년 전 조회 4,194
12년 전 조회 3,759
12년 전 조회 1,376
12년 전 조회 1,446
12년 전 조회 3,166
12년 전 조회 4,000
12년 전 조회 4,407
12년 전 조회 1,621
12년 전 조회 1,307
12년 전 조회 897
12년 전 조회 995
12년 전 조회 2,735
12년 전 조회 2,748
12년 전 조회 1,272
12년 전 조회 2,817
12년 전 조회 1,290
12년 전 조회 8,476
12년 전 조회 1,527
12년 전 조회 3,784
12년 전 조회 6,399
12년 전 조회 1,581
12년 전 조회 2,851
12년 전 조회 598
12년 전 조회 1,311
12년 전 조회 1,170
12년 전 조회 1,329
12년 전 조회 3,324
12년 전 조회 1,098
12년 전 조회 1,831
12년 전 조회 1,270
12년 전 조회 921
12년 전 조회 1,405
12년 전 조회 2,119
12년 전 조회 1,401
12년 전 조회 1,282
12년 전 조회 1,805
12년 전 조회 1.5만
12년 전 조회 1,081
12년 전 조회 883
12년 전 조회 2,728
12년 전 조회 8,972
12년 전 조회 1,296
12년 전 조회 1,185
12년 전 조회 1,646
12년 전 조회 1,153
12년 전 조회 1,283
12년 전 조회 3,568
12년 전 조회 1,476
12년 전 조회 2,142
12년 전 조회 1,168
12년 전 조회 1,330
12년 전 조회 3,045
12년 전 조회 889
12년 전 조회 1,356
12년 전 조회 1,621
12년 전 조회 2,050
12년 전 조회 2,149
12년 전 조회 2,579
12년 전 조회 1,071
12년 전 조회 1,094
12년 전 조회 4,460
12년 전 조회 1,509
12년 전 조회 1,651
12년 전 조회 5,510
12년 전 조회 3,275
12년 전 조회 2,156
12년 전 조회 1,315
12년 전 조회 3,503
12년 전 조회 2,534
12년 전 조회 2,241
12년 전 조회 1,860
12년 전 조회 1,556
12년 전 조회 1,549
12년 전 조회 1,301
12년 전 조회 2,419
12년 전 조회 1,692
12년 전 조회 1,367
12년 전 조회 2,450
12년 전 조회 1,631
12년 전 조회 2,244
12년 전 조회 3,460
12년 전 조회 1,250
12년 전 조회 2,217
12년 전 조회 1,055
12년 전 조회 1,545
12년 전 조회 2,136
12년 전 조회 1,770
12년 전 조회 4,578
12년 전 조회 2,678
12년 전 조회 2,848
12년 전 조회 1,241
12년 전 조회 8,557
12년 전 조회 3,354
12년 전 조회 1,450
12년 전 조회 1,304
12년 전 조회 2,703
12년 전 조회 5,380
12년 전 조회 3,431
🐛 버그신고