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

· 12년 전 · 1481

<?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,719
12년 전 조회 8,598
12년 전 조회 4,200
12년 전 조회 3,767
12년 전 조회 1,383
12년 전 조회 1,455
12년 전 조회 3,170
12년 전 조회 4,007
12년 전 조회 4,413
12년 전 조회 1,631
12년 전 조회 1,315
12년 전 조회 902
12년 전 조회 1,004
12년 전 조회 2,745
12년 전 조회 2,757
12년 전 조회 1,277
12년 전 조회 2,822
12년 전 조회 1,302
12년 전 조회 8,481
12년 전 조회 1,531
12년 전 조회 3,795
12년 전 조회 6,407
12년 전 조회 1,587
12년 전 조회 2,855
12년 전 조회 605
12년 전 조회 1,316
12년 전 조회 1,178
12년 전 조회 1,338
12년 전 조회 3,333
12년 전 조회 1,104
12년 전 조회 1,839
12년 전 조회 1,277
12년 전 조회 926
12년 전 조회 1,415
12년 전 조회 2,123
12년 전 조회 1,407
12년 전 조회 1,292
12년 전 조회 1,813
12년 전 조회 1.5만
12년 전 조회 1,089
12년 전 조회 888
12년 전 조회 2,734
12년 전 조회 8,981
12년 전 조회 1,299
12년 전 조회 1,194
12년 전 조회 1,654
12년 전 조회 1,159
12년 전 조회 1,293
12년 전 조회 3,573
12년 전 조회 1,482
12년 전 조회 2,147
12년 전 조회 1,172
12년 전 조회 1,338
12년 전 조회 3,050
12년 전 조회 896
12년 전 조회 1,365
12년 전 조회 1,632
12년 전 조회 2,056
12년 전 조회 2,156
12년 전 조회 2,584
12년 전 조회 1,075
12년 전 조회 1,100
12년 전 조회 4,463
12년 전 조회 1,517
12년 전 조회 1,658
12년 전 조회 5,517
12년 전 조회 3,282
12년 전 조회 2,162
12년 전 조회 1,320
12년 전 조회 3,508
12년 전 조회 2,540
12년 전 조회 2,247
12년 전 조회 1,865
12년 전 조회 1,559
12년 전 조회 1,554
12년 전 조회 1,305
12년 전 조회 2,425
12년 전 조회 1,697
12년 전 조회 1,374
12년 전 조회 2,459
12년 전 조회 1,636
12년 전 조회 2,246
12년 전 조회 3,470
12년 전 조회 1,258
12년 전 조회 2,226
12년 전 조회 1,058
12년 전 조회 1,549
12년 전 조회 2,139
12년 전 조회 1,778
12년 전 조회 4,584
12년 전 조회 2,684
12년 전 조회 2,853
12년 전 조회 1,252
12년 전 조회 8,560
12년 전 조회 3,356
12년 전 조회 1,459
12년 전 조회 1,309
12년 전 조회 2,711
12년 전 조회 5,386
12년 전 조회 3,437
🐛 버그신고