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

· 12년 전 · 1498

<?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,740
12년 전 조회 8,620
12년 전 조회 4,222
12년 전 조회 3,788
12년 전 조회 1,400
12년 전 조회 1,474
12년 전 조회 3,186
12년 전 조회 4,027
12년 전 조회 4,424
12년 전 조회 1,646
12년 전 조회 1,333
12년 전 조회 919
12년 전 조회 1,022
12년 전 조회 2,757
12년 전 조회 2,773
12년 전 조회 1,296
12년 전 조회 2,833
12년 전 조회 1,321
12년 전 조회 8,496
12년 전 조회 1,545
12년 전 조회 3,808
12년 전 조회 6,425
12년 전 조회 1,609
12년 전 조회 2,872
12년 전 조회 627
12년 전 조회 1,336
12년 전 조회 1,191
12년 전 조회 1,362
12년 전 조회 3,351
12년 전 조회 1,127
12년 전 조회 1,854
12년 전 조회 1,292
12년 전 조회 942
12년 전 조회 1,425
12년 전 조회 2,139
12년 전 조회 1,423
12년 전 조회 1,308
12년 전 조회 1,833
12년 전 조회 1.5만
12년 전 조회 1,104
12년 전 조회 908
12년 전 조회 2,748
12년 전 조회 9,000
12년 전 조회 1,316
12년 전 조회 1,208
12년 전 조회 1,674
12년 전 조회 1,172
12년 전 조회 1,315
12년 전 조회 3,592
12년 전 조회 1,499
12년 전 조회 2,166
12년 전 조회 1,188
12년 전 조회 1,345
12년 전 조회 3,064
12년 전 조회 911
12년 전 조회 1,386
12년 전 조회 1,651
12년 전 조회 2,072
12년 전 조회 2,170
12년 전 조회 2,604
12년 전 조회 1,091
12년 전 조회 1,115
12년 전 조회 4,482
12년 전 조회 1,534
12년 전 조회 1,679
12년 전 조회 5,539
12년 전 조회 3,296
12년 전 조회 2,175
12년 전 조회 1,332
12년 전 조회 3,522
12년 전 조회 2,557
12년 전 조회 2,254
12년 전 조회 1,877
12년 전 조회 1,575
12년 전 조회 1,575
12년 전 조회 1,322
12년 전 조회 2,440
12년 전 조회 1,711
12년 전 조회 1,395
12년 전 조회 2,468
12년 전 조회 1,647
12년 전 조회 2,260
12년 전 조회 3,483
12년 전 조회 1,268
12년 전 조회 2,241
12년 전 조회 1,070
12년 전 조회 1,563
12년 전 조회 2,148
12년 전 조회 1,794
12년 전 조회 4,598
12년 전 조회 2,694
12년 전 조회 2,874
12년 전 조회 1,258
12년 전 조회 8,571
12년 전 조회 3,364
12년 전 조회 1,471
12년 전 조회 1,321
12년 전 조회 2,722
12년 전 조회 5,398
12년 전 조회 3,450
🐛 버그신고