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

· 12년 전 · 1474

<?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,708
12년 전 조회 8,592
12년 전 조회 4,193
12년 전 조회 3,758
12년 전 조회 1,376
12년 전 조회 1,446
12년 전 조회 3,166
12년 전 조회 3,998
12년 전 조회 4,406
12년 전 조회 1,620
12년 전 조회 1,306
12년 전 조회 896
12년 전 조회 994
12년 전 조회 2,732
12년 전 조회 2,745
12년 전 조회 1,269
12년 전 조회 2,816
12년 전 조회 1,289
12년 전 조회 8,474
12년 전 조회 1,526
12년 전 조회 3,783
12년 전 조회 6,398
12년 전 조회 1,581
12년 전 조회 2,848
12년 전 조회 595
12년 전 조회 1,310
12년 전 조회 1,168
12년 전 조회 1,329
12년 전 조회 3,321
12년 전 조회 1,095
12년 전 조회 1,829
12년 전 조회 1,270
12년 전 조회 919
12년 전 조회 1,404
12년 전 조회 2,118
12년 전 조회 1,401
12년 전 조회 1,280
12년 전 조회 1,803
12년 전 조회 1.5만
12년 전 조회 1,080
12년 전 조회 880
12년 전 조회 2,726
12년 전 조회 8,972
12년 전 조회 1,294
12년 전 조회 1,185
12년 전 조회 1,643
12년 전 조회 1,152
12년 전 조회 1,282
12년 전 조회 3,566
12년 전 조회 1,475
12년 전 조회 2,140
12년 전 조회 1,167
12년 전 조회 1,329
12년 전 조회 3,045
12년 전 조회 888
12년 전 조회 1,353
12년 전 조회 1,621
12년 전 조회 2,050
12년 전 조회 2,147
12년 전 조회 2,577
12년 전 조회 1,067
12년 전 조회 1,093
12년 전 조회 4,458
12년 전 조회 1,507
12년 전 조회 1,647
12년 전 조회 5,507
12년 전 조회 3,273
12년 전 조회 2,152
12년 전 조회 1,315
12년 전 조회 3,501
12년 전 조회 2,534
12년 전 조회 2,240
12년 전 조회 1,860
12년 전 조회 1,552
12년 전 조회 1,546
12년 전 조회 1,301
12년 전 조회 2,416
12년 전 조회 1,691
12년 전 조회 1,366
12년 전 조회 2,448
12년 전 조회 1,631
12년 전 조회 2,243
12년 전 조회 3,459
12년 전 조회 1,247
12년 전 조회 2,217
12년 전 조회 1,055
12년 전 조회 1,544
12년 전 조회 2,134
12년 전 조회 1,767
12년 전 조회 4,577
12년 전 조회 2,676
12년 전 조회 2,844
12년 전 조회 1,237
12년 전 조회 8,557
12년 전 조회 3,352
12년 전 조회 1,449
12년 전 조회 1,303
12년 전 조회 2,700
12년 전 조회 5,379
12년 전 조회 3,431
🐛 버그신고