php변수를 스크립트로 사용하는 방법

· 12년 전 · 2396 · 2
<?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) 17회 2013-08-22 21:11
|

댓글 2개

감사해요 ㅎㅎ
유용한 정보네요
댓글을 작성하시려면 로그인이 필요합니다. 로그인

프로그램

+
제목 글쓴이 날짜 조회
12년 전 조회 1,532
12년 전 조회 1,382
12년 전 조회 3,916
12년 전 조회 1,537
12년 전 조회 870
12년 전 조회 1,568
12년 전 조회 660
12년 전 조회 934
12년 전 조회 1,325
12년 전 조회 1,216
12년 전 조회 5,187
12년 전 조회 2,542
12년 전 조회 1,808
12년 전 조회 648
12년 전 조회 2,254
12년 전 조회 850
12년 전 조회 1,166
12년 전 조회 881
12년 전 조회 3,385
12년 전 조회 804
12년 전 조회 3,692
12년 전 조회 1,378
12년 전 조회 1,661
12년 전 조회 2,277
12년 전 조회 2,338
12년 전 조회 597
12년 전 조회 673
12년 전 조회 1,331
12년 전 조회 643
12년 전 조회 824
12년 전 조회 6,604
12년 전 조회 578
12년 전 조회 869
12년 전 조회 511
12년 전 조회 553
12년 전 조회 815
12년 전 조회 758
12년 전 조회 1,753
12년 전 조회 1,744
12년 전 조회 1,384
12년 전 조회 5,252
12년 전 조회 1,524
12년 전 조회 1,625
12년 전 조회 977
12년 전 조회 1,993
12년 전 조회 1,393
12년 전 조회 3,056
12년 전 조회 2,566
12년 전 조회 1,922
12년 전 조회 2,397
12년 전 조회 1,879
12년 전 조회 1,652
12년 전 조회 1,049
12년 전 조회 1,163
12년 전 조회 5,580
12년 전 조회 2,626
12년 전 조회 3,105
12년 전 조회 1,765
12년 전 조회 1,477
12년 전 조회 829
12년 전 조회 1,840
12년 전 조회 944
12년 전 조회 1,521
12년 전 조회 2,789
12년 전 조회 2,355
12년 전 조회 1,699
12년 전 조회 6,679
12년 전 조회 2,508
12년 전 조회 728
12년 전 조회 681
12년 전 조회 1,431
12년 전 조회 2,074
12년 전 조회 916
12년 전 조회 723
12년 전 조회 1,356
12년 전 조회 1,490
12년 전 조회 717
12년 전 조회 1,142
12년 전 조회 1,417
12년 전 조회 806
12년 전 조회 1,101
12년 전 조회 3,688
12년 전 조회 1,438
12년 전 조회 979
12년 전 조회 865
12년 전 조회 1,232
12년 전 조회 1,467
12년 전 조회 4,253
12년 전 조회 5,588
12년 전 조회 717
12년 전 조회 2,284
12년 전 조회 6,754
12년 전 조회 2,426
12년 전 조회 9,347
12년 전 조회 553
12년 전 조회 755
12년 전 조회 1,368
12년 전 조회 1,685
12년 전 조회 1,338
12년 전 조회 1,952
🐛 버그신고