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

· 12년 전 · 1565

<?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,814
12년 전 조회 8,698
12년 전 조회 4,286
12년 전 조회 3,852
12년 전 조회 1,470
12년 전 조회 1,548
12년 전 조회 3,256
12년 전 조회 4,091
12년 전 조회 4,500
12년 전 조회 1,718
12년 전 조회 1,398
12년 전 조회 995
12년 전 조회 1,103
12년 전 조회 2,829
12년 전 조회 2,844
12년 전 조회 1,362
12년 전 조회 2,913
12년 전 조회 1,389
12년 전 조회 8,558
12년 전 조회 1,617
12년 전 조회 3,887
12년 전 조회 6,484
12년 전 조회 1,682
12년 전 조회 2,940
12년 전 조회 710
12년 전 조회 1,412
12년 전 조회 1,260
12년 전 조회 1,419
12년 전 조회 3,420
12년 전 조회 1,194
12년 전 조회 1,921
12년 전 조회 1,361
12년 전 조회 1,015
12년 전 조회 1,501
12년 전 조회 2,218
12년 전 조회 1,503
12년 전 조회 1,379
12년 전 조회 1,904
12년 전 조회 1.5만
12년 전 조회 1,176
12년 전 조회 987
12년 전 조회 2,821
12년 전 조회 9,065
12년 전 조회 1,379
12년 전 조회 1,286
12년 전 조회 1,743
12년 전 조회 1,256
12년 전 조회 1,388
12년 전 조회 3,658
12년 전 조회 1,566
12년 전 조회 2,237
12년 전 조회 1,258
12년 전 조회 1,407
12년 전 조회 3,138
12년 전 조회 988
12년 전 조회 1,455
12년 전 조회 1,714
12년 전 조회 2,133
12년 전 조회 2,232
12년 전 조회 2,672
12년 전 조회 1,162
12년 전 조회 1,192
12년 전 조회 4,558
12년 전 조회 1,597
12년 전 조회 1,743
12년 전 조회 5,621
12년 전 조회 3,367
12년 전 조회 2,253
12년 전 조회 1,400
12년 전 조회 3,595
12년 전 조회 2,615
12년 전 조회 2,324
12년 전 조회 1,943
12년 전 조회 1,645
12년 전 조회 1,634
12년 전 조회 1,391
12년 전 조회 2,499
12년 전 조회 1,784
12년 전 조회 1,455
12년 전 조회 2,531
12년 전 조회 1,720
12년 전 조회 2,336
12년 전 조회 3,564
12년 전 조회 1,341
12년 전 조회 2,301
12년 전 조회 1,147
12년 전 조회 1,636
12년 전 조회 2,215
12년 전 조회 1,857
12년 전 조회 4,671
12년 전 조회 2,776
12년 전 조회 2,940
12년 전 조회 1,325
12년 전 조회 8,644
12년 전 조회 3,442
12년 전 조회 1,540
12년 전 조회 1,389
12년 전 조회 2,793
12년 전 조회 5,472
12년 전 조회 3,518
🐛 버그신고