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

· 12년 전 · 2382 · 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,515
12년 전 조회 1,367
12년 전 조회 3,899
12년 전 조회 1,519
12년 전 조회 857
12년 전 조회 1,551
12년 전 조회 644
12년 전 조회 915
12년 전 조회 1,310
12년 전 조회 1,196
12년 전 조회 5,166
12년 전 조회 2,520
12년 전 조회 1,786
12년 전 조회 631
12년 전 조회 2,231
12년 전 조회 830
12년 전 조회 1,143
12년 전 조회 862
12년 전 조회 3,367
12년 전 조회 778
12년 전 조회 3,675
12년 전 조회 1,361
12년 전 조회 1,643
12년 전 조회 2,255
12년 전 조회 2,322
12년 전 조회 578
12년 전 조회 654
12년 전 조회 1,311
12년 전 조회 625
12년 전 조회 808
12년 전 조회 6,581
12년 전 조회 564
12년 전 조회 851
12년 전 조회 492
12년 전 조회 535
12년 전 조회 795
12년 전 조회 737
12년 전 조회 1,739
12년 전 조회 1,729
12년 전 조회 1,368
12년 전 조회 5,235
12년 전 조회 1,511
12년 전 조회 1,608
12년 전 조회 960
12년 전 조회 1,978
12년 전 조회 1,375
12년 전 조회 3,044
12년 전 조회 2,548
12년 전 조회 1,905
12년 전 조회 2,383
12년 전 조회 1,861
12년 전 조회 1,633
12년 전 조회 1,030
12년 전 조회 1,141
12년 전 조회 5,560
12년 전 조회 2,608
12년 전 조회 3,090
12년 전 조회 1,747
12년 전 조회 1,461
12년 전 조회 811
12년 전 조회 1,818
12년 전 조회 928
12년 전 조회 1,505
12년 전 조회 2,770
12년 전 조회 2,341
12년 전 조회 1,681
12년 전 조회 6,661
12년 전 조회 2,497
12년 전 조회 712
12년 전 조회 668
12년 전 조회 1,413
12년 전 조회 2,057
12년 전 조회 900
12년 전 조회 706
12년 전 조회 1,337
12년 전 조회 1,473
12년 전 조회 700
12년 전 조회 1,124
12년 전 조회 1,400
12년 전 조회 788
12년 전 조회 1,079
12년 전 조회 3,675
12년 전 조회 1,421
12년 전 조회 956
12년 전 조회 850
12년 전 조회 1,217
12년 전 조회 1,450
12년 전 조회 4,241
12년 전 조회 5,571
12년 전 조회 700
12년 전 조회 2,267
12년 전 조회 6,736
12년 전 조회 2,410
12년 전 조회 9,330
12년 전 조회 536
12년 전 조회 738
12년 전 조회 1,357
12년 전 조회 1,673
12년 전 조회 1,326
12년 전 조회 1,934
🐛 버그신고