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

· 12년 전 · 1479

<?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,716
12년 전 조회 8,595
12년 전 조회 4,198
12년 전 조회 3,764
12년 전 조회 1,378
12년 전 조회 1,450
12년 전 조회 3,170
12년 전 조회 4,003
12년 전 조회 4,413
12년 전 조회 1,628
12년 전 조회 1,312
12년 전 조회 899
12년 전 조회 999
12년 전 조회 2,744
12년 전 조회 2,754
12년 전 조회 1,276
12년 전 조회 2,821
12년 전 조회 1,300
12년 전 조회 8,479
12년 전 조회 1,529
12년 전 조회 3,792
12년 전 조회 6,405
12년 전 조회 1,587
12년 전 조회 2,854
12년 전 조회 602
12년 전 조회 1,315
12년 전 조회 1,176
12년 전 조회 1,337
12년 전 조회 3,330
12년 전 조회 1,102
12년 전 조회 1,837
12년 전 조회 1,276
12년 전 조회 925
12년 전 조회 1,412
12년 전 조회 2,123
12년 전 조회 1,405
12년 전 조회 1,289
12년 전 조회 1,811
12년 전 조회 1.5만
12년 전 조회 1,086
12년 전 조회 887
12년 전 조회 2,733
12년 전 조회 8,979
12년 전 조회 1,298
12년 전 조회 1,193
12년 전 조회 1,652
12년 전 조회 1,158
12년 전 조회 1,289
12년 전 조회 3,571
12년 전 조회 1,480
12년 전 조회 2,146
12년 전 조회 1,170
12년 전 조회 1,335
12년 전 조회 3,048
12년 전 조회 894
12년 전 조회 1,363
12년 전 조회 1,629
12년 전 조회 2,056
12년 전 조회 2,153
12년 전 조회 2,581
12년 전 조회 1,073
12년 전 조회 1,100
12년 전 조회 4,462
12년 전 조회 1,516
12년 전 조회 1,654
12년 전 조회 5,515
12년 전 조회 3,280
12년 전 조회 2,162
12년 전 조회 1,320
12년 전 조회 3,507
12년 전 조회 2,540
12년 전 조회 2,244
12년 전 조회 1,863
12년 전 조회 1,559
12년 전 조회 1,550
12년 전 조회 1,304
12년 전 조회 2,424
12년 전 조회 1,697
12년 전 조회 1,373
12년 전 조회 2,456
12년 전 조회 1,635
12년 전 조회 2,245
12년 전 조회 3,467
12년 전 조회 1,256
12년 전 조회 2,224
12년 전 조회 1,058
12년 전 조회 1,549
12년 전 조회 2,139
12년 전 조회 1,776
12년 전 조회 4,583
12년 전 조회 2,684
12년 전 조회 2,852
12년 전 조회 1,252
12년 전 조회 8,560
12년 전 조회 3,354
12년 전 조회 1,456
12년 전 조회 1,308
12년 전 조회 2,711
12년 전 조회 5,384
12년 전 조회 3,436
🐛 버그신고