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

· 12년 전 · 1531

<?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,775
12년 전 조회 8,652
12년 전 조회 4,251
12년 전 조회 3,822
12년 전 조회 1,429
12년 전 조회 1,506
12년 전 조회 3,222
12년 전 조회 4,053
12년 전 조회 4,463
12년 전 조회 1,683
12년 전 조회 1,365
12년 전 조회 960
12년 전 조회 1,058
12년 전 조회 2,797
12년 전 조회 2,805
12년 전 조회 1,320
12년 전 조회 2,869
12년 전 조회 1,351
12년 전 조회 8,528
12년 전 조회 1,579
12년 전 조회 3,844
12년 전 조회 6,454
12년 전 조회 1,637
12년 전 조회 2,899
12년 전 조회 664
12년 전 조회 1,368
12년 전 조회 1,220
12년 전 조회 1,381
12년 전 조회 3,381
12년 전 조회 1,158
12년 전 조회 1,885
12년 전 조회 1,321
12년 전 조회 975
12년 전 조회 1,461
12년 전 조회 2,169
12년 전 조회 1,454
12년 전 조회 1,336
12년 전 조회 1,864
12년 전 조회 1.5만
12년 전 조회 1,131
12년 전 조회 953
12년 전 조회 2,786
12년 전 조회 9,028
12년 전 조회 1,343
12년 전 조회 1,239
12년 전 조회 1,698
12년 전 조회 1,215
12년 전 조회 1,354
12년 전 조회 3,622
12년 전 조회 1,532
12년 전 조회 2,200
12년 전 조회 1,218
12년 전 조회 1,366
12년 전 조회 3,094
12년 전 조회 948
12년 전 조회 1,416
12년 전 조회 1,677
12년 전 조회 2,100
12년 전 조회 2,199
12년 전 조회 2,637
12년 전 조회 1,117
12년 전 조회 1,145
12년 전 조회 4,519
12년 전 조회 1,559
12년 전 조회 1,707
12년 전 조회 5,571
12년 전 조회 3,329
12년 전 조회 2,205
12년 전 조회 1,371
12년 전 조회 3,556
12년 전 조회 2,585
12년 전 조회 2,284
12년 전 조회 1,903
12년 전 조회 1,607
12년 전 조회 1,598
12년 전 조회 1,350
12년 전 조회 2,464
12년 전 조회 1,735
12년 전 조회 1,423
12년 전 조회 2,495
12년 전 조회 1,680
12년 전 조회 2,296
12년 전 조회 3,516
12년 전 조회 1,302
12년 전 조회 2,270
12년 전 조회 1,112
12년 전 조회 1,593
12년 전 조회 2,182
12년 전 조회 1,823
12년 전 조회 4,628
12년 전 조회 2,728
12년 전 조회 2,904
12년 전 조회 1,292
12년 전 조회 8,597
12년 전 조회 3,400
12년 전 조회 1,496
12년 전 조회 1,348
12년 전 조회 2,750
12년 전 조회 5,427
12년 전 조회 3,474
🐛 버그신고