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

· 12년 전 · 2384 · 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,517
12년 전 조회 1,368
12년 전 조회 3,899
12년 전 조회 1,519
12년 전 조회 858
12년 전 조회 1,552
12년 전 조회 645
12년 전 조회 916
12년 전 조회 1,312
12년 전 조회 1,197
12년 전 조회 5,169
12년 전 조회 2,522
12년 전 조회 1,786
12년 전 조회 631
12년 전 조회 2,232
12년 전 조회 831
12년 전 조회 1,145
12년 전 조회 863
12년 전 조회 3,369
12년 전 조회 779
12년 전 조회 3,676
12년 전 조회 1,361
12년 전 조회 1,644
12년 전 조회 2,256
12년 전 조회 2,323
12년 전 조회 579
12년 전 조회 654
12년 전 조회 1,311
12년 전 조회 626
12년 전 조회 809
12년 전 조회 6,585
12년 전 조회 565
12년 전 조회 852
12년 전 조회 493
12년 전 조회 536
12년 전 조회 796
12년 전 조회 740
12년 전 조회 1,739
12년 전 조회 1,729
12년 전 조회 1,369
12년 전 조회 5,237
12년 전 조회 1,511
12년 전 조회 1,610
12년 전 조회 963
12년 전 조회 1,980
12년 전 조회 1,377
12년 전 조회 3,046
12년 전 조회 2,551
12년 전 조회 1,907
12년 전 조회 2,385
12년 전 조회 1,862
12년 전 조회 1,633
12년 전 조회 1,031
12년 전 조회 1,141
12년 전 조회 5,561
12년 전 조회 2,608
12년 전 조회 3,090
12년 전 조회 1,747
12년 전 조회 1,464
12년 전 조회 812
12년 전 조회 1,818
12년 전 조회 929
12년 전 조회 1,507
12년 전 조회 2,771
12년 전 조회 2,341
12년 전 조회 1,682
12년 전 조회 6,663
12년 전 조회 2,498
12년 전 조회 713
12년 전 조회 668
12년 전 조회 1,414
12년 전 조회 2,058
12년 전 조회 901
12년 전 조회 707
12년 전 조회 1,339
12년 전 조회 1,474
12년 전 조회 701
12년 전 조회 1,126
12년 전 조회 1,401
12년 전 조회 788
12년 전 조회 1,080
12년 전 조회 3,675
12년 전 조회 1,423
12년 전 조회 957
12년 전 조회 851
12년 전 조회 1,217
12년 전 조회 1,452
12년 전 조회 4,241
12년 전 조회 5,573
12년 전 조회 702
12년 전 조회 2,268
12년 전 조회 6,737
12년 전 조회 2,411
12년 전 조회 9,332
12년 전 조회 537
12년 전 조회 739
12년 전 조회 1,358
12년 전 조회 1,674
12년 전 조회 1,328
12년 전 조회 1,935
🐛 버그신고