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

· 12년 전 · 2376 · 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,511
12년 전 조회 1,359
12년 전 조회 3,888
12년 전 조회 1,511
12년 전 조회 851
12년 전 조회 1,546
12년 전 조회 635
12년 전 조회 910
12년 전 조회 1,300
12년 전 조회 1,188
12년 전 조회 5,158
12년 전 조회 2,512
12년 전 조회 1,775
12년 전 조회 624
12년 전 조회 2,225
12년 전 조회 824
12년 전 조회 1,138
12년 전 조회 851
12년 전 조회 3,364
12년 전 조회 771
12년 전 조회 3,666
12년 전 조회 1,356
12년 전 조회 1,635
12년 전 조회 2,247
12년 전 조회 2,315
12년 전 조회 572
12년 전 조회 648
12년 전 조회 1,304
12년 전 조회 619
12년 전 조회 801
12년 전 조회 6,578
12년 전 조회 556
12년 전 조회 846
12년 전 조회 488
12년 전 조회 528
12년 전 조회 790
12년 전 조회 733
12년 전 조회 1,730
12년 전 조회 1,720
12년 전 조회 1,359
12년 전 조회 5,231
12년 전 조회 1,500
12년 전 조회 1,604
12년 전 조회 954
12년 전 조회 1,973
12년 전 조회 1,370
12년 전 조회 3,039
12년 전 조회 2,544
12년 전 조회 1,898
12년 전 조회 2,377
12년 전 조회 1,852
12년 전 조회 1,626
12년 전 조회 1,024
12년 전 조회 1,136
12년 전 조회 5,551
12년 전 조회 2,602
12년 전 조회 3,081
12년 전 조회 1,743
12년 전 조회 1,456
12년 전 조회 803
12년 전 조회 1,812
12년 전 조회 920
12년 전 조회 1,500
12년 전 조회 2,759
12년 전 조회 2,335
12년 전 조회 1,673
12년 전 조회 6,654
12년 전 조회 2,493
12년 전 조회 706
12년 전 조회 659
12년 전 조회 1,405
12년 전 조회 2,046
12년 전 조회 892
12년 전 조회 700
12년 전 조회 1,329
12년 전 조회 1,464
12년 전 조회 695
12년 전 조회 1,118
12년 전 조회 1,394
12년 전 조회 784
12년 전 조회 1,072
12년 전 조회 3,668
12년 전 조회 1,418
12년 전 조회 952
12년 전 조회 842
12년 전 조회 1,210
12년 전 조회 1,444
12년 전 조회 4,232
12년 전 조회 5,564
12년 전 조회 694
12년 전 조회 2,261
12년 전 조회 6,731
12년 전 조회 2,401
12년 전 조회 9,324
12년 전 조회 528
12년 전 조회 732
12년 전 조회 1,347
12년 전 조회 1,666
12년 전 조회 1,320
12년 전 조회 1,927
🐛 버그신고