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

· 12년 전 · 1483

<?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,722
12년 전 조회 8,603
12년 전 조회 4,205
12년 전 조회 3,772
12년 전 조회 1,386
12년 전 조회 1,457
12년 전 조회 3,172
12년 전 조회 4,011
12년 전 조회 4,413
12년 전 조회 1,635
12년 전 조회 1,319
12년 전 조회 905
12년 전 조회 1,007
12년 전 조회 2,747
12년 전 조회 2,760
12년 전 조회 1,278
12년 전 조회 2,824
12년 전 조회 1,305
12년 전 조회 8,483
12년 전 조회 1,533
12년 전 조회 3,797
12년 전 조회 6,410
12년 전 조회 1,590
12년 전 조회 2,859
12년 전 조회 607
12년 전 조회 1,321
12년 전 조회 1,182
12년 전 조회 1,340
12년 전 조회 3,339
12년 전 조회 1,108
12년 전 조회 1,844
12년 전 조회 1,280
12년 전 조회 927
12년 전 조회 1,418
12년 전 조회 2,127
12년 전 조회 1,411
12년 전 조회 1,292
12년 전 조회 1,815
12년 전 조회 1.5만
12년 전 조회 1,091
12년 전 조회 893
12년 전 조회 2,739
12년 전 조회 8,983
12년 전 조회 1,303
12년 전 조회 1,196
12년 전 조회 1,657
12년 전 조회 1,160
12년 전 조회 1,297
12년 전 조회 3,576
12년 전 조회 1,484
12년 전 조회 2,151
12년 전 조회 1,177
12년 전 조회 1,339
12년 전 조회 3,054
12년 전 조회 897
12년 전 조회 1,368
12년 전 조회 1,635
12년 전 조회 2,060
12년 전 조회 2,157
12년 전 조회 2,590
12년 전 조회 1,078
12년 전 조회 1,104
12년 전 조회 4,467
12년 전 조회 1,520
12년 전 조회 1,660
12년 전 조회 5,523
12년 전 조회 3,287
12년 전 조회 2,166
12년 전 조회 1,322
12년 전 조회 3,511
12년 전 조회 2,544
12년 전 조회 2,252
12년 전 조회 1,868
12년 전 조회 1,562
12년 전 조회 1,556
12년 전 조회 1,310
12년 전 조회 2,428
12년 전 조회 1,701
12년 전 조회 1,375
12년 전 조회 2,462
12년 전 조회 1,639
12년 전 조회 2,250
12년 전 조회 3,471
12년 전 조회 1,262
12년 전 조회 2,228
12년 전 조회 1,062
12년 전 조회 1,550
12년 전 조회 2,141
12년 전 조회 1,781
12년 전 조회 4,587
12년 전 조회 2,687
12년 전 조회 2,855
12년 전 조회 1,254
12년 전 조회 8,561
12년 전 조회 3,358
12년 전 조회 1,464
12년 전 조회 1,311
12년 전 조회 2,712
12년 전 조회 5,388
12년 전 조회 3,439
🐛 버그신고