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

· 12년 전 · 2484 · 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,618
12년 전 조회 1,487
12년 전 조회 4,013
12년 전 조회 1,641
12년 전 조회 968
12년 전 조회 1,648
12년 전 조회 746
12년 전 조회 1,028
12년 전 조회 1,421
12년 전 조회 1,317
12년 전 조회 5,278
12년 전 조회 2,622
12년 전 조회 1,895
12년 전 조회 735
12년 전 조회 2,349
12년 전 조회 947
12년 전 조회 1,258
12년 전 조회 962
12년 전 조회 3,482
12년 전 조회 878
12년 전 조회 3,788
12년 전 조회 1,460
12년 전 조회 1,755
12년 전 조회 2,369
12년 전 조회 2,429
12년 전 조회 690
12년 전 조회 761
12년 전 조회 1,411
12년 전 조회 731
12년 전 조회 919
12년 전 조회 6,679
12년 전 조회 668
12년 전 조회 954
12년 전 조회 602
12년 전 조회 644
12년 전 조회 903
12년 전 조회 850
12년 전 조회 1,841
12년 전 조회 1,835
12년 전 조회 1,475
12년 전 조회 5,346
12년 전 조회 1,617
12년 전 조회 1,713
12년 전 조회 1,064
12년 전 조회 2,083
12년 전 조회 1,475
12년 전 조회 3,146
12년 전 조회 2,663
12년 전 조회 2,007
12년 전 조회 2,485
12년 전 조회 1,967
12년 전 조회 1,729
12년 전 조회 1,148
12년 전 조회 1,252
12년 전 조회 5,655
12년 전 조회 2,711
12년 전 조회 3,184
12년 전 조회 1,839
12년 전 조회 1,562
12년 전 조회 917
12년 전 조회 1,927
12년 전 조회 1,038
12년 전 조회 1,606
12년 전 조회 2,873
12년 전 조회 2,442
12년 전 조회 1,785
12년 전 조회 6,762
12년 전 조회 2,599
12년 전 조회 816
12년 전 조회 771
12년 전 조회 1,508
12년 전 조회 2,166
12년 전 조회 984
12년 전 조회 809
12년 전 조회 1,437
12년 전 조회 1,579
12년 전 조회 798
12년 전 조회 1,235
12년 전 조회 1,512
12년 전 조회 893
12년 전 조회 1,183
12년 전 조회 3,760
12년 전 조회 1,526
12년 전 조회 1,056
12년 전 조회 955
12년 전 조회 1,315
12년 전 조회 1,555
12년 전 조회 4,342
12년 전 조회 5,674
12년 전 조회 805
12년 전 조회 2,373
12년 전 조회 6,841
12년 전 조회 2,501
12년 전 조회 9,436
12년 전 조회 633
12년 전 조회 829
12년 전 조회 1,463
12년 전 조회 1,765
12년 전 조회 1,428
12년 전 조회 2,034
🐛 버그신고