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

· 12년 전 · 2364 · 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,503
12년 전 조회 1,352
12년 전 조회 3,885
12년 전 조회 1,504
12년 전 조회 840
12년 전 조회 1,539
12년 전 조회 630
12년 전 조회 903
12년 전 조회 1,295
12년 전 조회 1,180
12년 전 조회 5,153
12년 전 조회 2,508
12년 전 조회 1,767
12년 전 조회 617
12년 전 조회 2,223
12년 전 조회 813
12년 전 조회 1,130
12년 전 조회 843
12년 전 조회 3,359
12년 전 조회 765
12년 전 조회 3,659
12년 전 조회 1,344
12년 전 조회 1,625
12년 전 조회 2,242
12년 전 조회 2,304
12년 전 조회 564
12년 전 조회 639
12년 전 조회 1,296
12년 전 조회 612
12년 전 조회 790
12년 전 조회 6,575
12년 전 조회 547
12년 전 조회 836
12년 전 조회 476
12년 전 조회 521
12년 전 조회 787
12년 전 조회 722
12년 전 조회 1,721
12년 전 조회 1,711
12년 전 조회 1,350
12년 전 조회 5,221
12년 전 조회 1,492
12년 전 조회 1,594
12년 전 조회 947
12년 전 조회 1,967
12년 전 조회 1,360
12년 전 조회 3,036
12년 전 조회 2,541
12년 전 조회 1,887
12년 전 조회 2,365
12년 전 조회 1,846
12년 전 조회 1,619
12년 전 조회 1,014
12년 전 조회 1,124
12년 전 조회 5,547
12년 전 조회 2,591
12년 전 조회 3,073
12년 전 조회 1,739
12년 전 조회 1,453
12년 전 조회 794
12년 전 조회 1,800
12년 전 조회 911
12년 전 조회 1,489
12년 전 조회 2,750
12년 전 조회 2,324
12년 전 조회 1,666
12년 전 조회 6,644
12년 전 조회 2,487
12년 전 조회 693
12년 전 조회 651
12년 전 조회 1,401
12년 전 조회 2,043
12년 전 조회 884
12년 전 조회 692
12년 전 조회 1,322
12년 전 조회 1,455
12년 전 조회 685
12년 전 조회 1,114
12년 전 조회 1,386
12년 전 조회 776
12년 전 조회 1,066
12년 전 조회 3,659
12년 전 조회 1,408
12년 전 조회 950
12년 전 조회 832
12년 전 조회 1,200
12년 전 조회 1,436
12년 전 조회 4,230
12년 전 조회 5,558
12년 전 조회 683
12년 전 조회 2,257
12년 전 조회 6,723
12년 전 조회 2,397
12년 전 조회 9,316
12년 전 조회 519
12년 전 조회 720
12년 전 조회 1,342
12년 전 조회 1,657
12년 전 조회 1,310
12년 전 조회 1,920
🐛 버그신고