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

· 12년 전 · 1473

<?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,706
12년 전 조회 8,592
12년 전 조회 4,190
12년 전 조회 3,757
12년 전 조회 1,375
12년 전 조회 1,442
12년 전 조회 3,164
12년 전 조회 3,997
12년 전 조회 4,406
12년 전 조회 1,619
12년 전 조회 1,306
12년 전 조회 893
12년 전 조회 992
12년 전 조회 2,731
12년 전 조회 2,744
12년 전 조회 1,269
12년 전 조회 2,813
12년 전 조회 1,289
12년 전 조회 8,473
12년 전 조회 1,526
12년 전 조회 3,783
12년 전 조회 6,397
12년 전 조회 1,579
12년 전 조회 2,845
12년 전 조회 595
12년 전 조회 1,308
12년 전 조회 1,167
12년 전 조회 1,329
12년 전 조회 3,321
12년 전 조회 1,094
12년 전 조회 1,828
12년 전 조회 1,268
12년 전 조회 919
12년 전 조회 1,403
12년 전 조회 2,118
12년 전 조회 1,398
12년 전 조회 1,279
12년 전 조회 1,802
12년 전 조회 1.5만
12년 전 조회 1,080
12년 전 조회 879
12년 전 조회 2,726
12년 전 조회 8,971
12년 전 조회 1,293
12년 전 조회 1,185
12년 전 조회 1,643
12년 전 조회 1,148
12년 전 조회 1,282
12년 전 조회 3,566
12년 전 조회 1,474
12년 전 조회 2,140
12년 전 조회 1,167
12년 전 조회 1,328
12년 전 조회 3,042
12년 전 조회 888
12년 전 조회 1,352
12년 전 조회 1,620
12년 전 조회 2,047
12년 전 조회 2,146
12년 전 조회 2,576
12년 전 조회 1,066
12년 전 조회 1,091
12년 전 조회 4,455
12년 전 조회 1,507
12년 전 조회 1,647
12년 전 조회 5,507
12년 전 조회 3,272
12년 전 조회 2,150
12년 전 조회 1,313
12년 전 조회 3,499
12년 전 조회 2,534
12년 전 조회 2,240
12년 전 조회 1,860
12년 전 조회 1,551
12년 전 조회 1,546
12년 전 조회 1,299
12년 전 조회 2,416
12년 전 조회 1,690
12년 전 조회 1,366
12년 전 조회 2,448
12년 전 조회 1,631
12년 전 조회 2,241
12년 전 조회 3,459
12년 전 조회 1,246
12년 전 조회 2,215
12년 전 조회 1,054
12년 전 조회 1,543
12년 전 조회 2,131
12년 전 조회 1,767
12년 전 조회 4,576
12년 전 조회 2,676
12년 전 조회 2,844
12년 전 조회 1,234
12년 전 조회 8,557
12년 전 조회 3,351
12년 전 조회 1,448
12년 전 조회 1,303
12년 전 조회 2,699
12년 전 조회 5,379
12년 전 조회 3,430
🐛 버그신고