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

· 12년 전 · 2489 · 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,620
12년 전 조회 1,490
12년 전 조회 4,015
12년 전 조회 1,641
12년 전 조회 969
12년 전 조회 1,651
12년 전 조회 748
12년 전 조회 1,030
12년 전 조회 1,424
12년 전 조회 1,318
12년 전 조회 5,281
12년 전 조회 2,623
12년 전 조회 1,896
12년 전 조회 737
12년 전 조회 2,350
12년 전 조회 948
12년 전 조회 1,261
12년 전 조회 964
12년 전 조회 3,484
12년 전 조회 882
12년 전 조회 3,790
12년 전 조회 1,463
12년 전 조회 1,759
12년 전 조회 2,372
12년 전 조회 2,430
12년 전 조회 691
12년 전 조회 764
12년 전 조회 1,412
12년 전 조회 734
12년 전 조회 923
12년 전 조회 6,682
12년 전 조회 670
12년 전 조회 957
12년 전 조회 605
12년 전 조회 646
12년 전 조회 905
12년 전 조회 852
12년 전 조회 1,844
12년 전 조회 1,840
12년 전 조회 1,479
12년 전 조회 5,349
12년 전 조회 1,622
12년 전 조회 1,719
12년 전 조회 1,065
12년 전 조회 2,086
12년 전 조회 1,477
12년 전 조회 3,147
12년 전 조회 2,666
12년 전 조회 2,011
12년 전 조회 2,490
12년 전 조회 1,967
12년 전 조회 1,733
12년 전 조회 1,149
12년 전 조회 1,254
12년 전 조회 5,656
12년 전 조회 2,715
12년 전 조회 3,188
12년 전 조회 1,841
12년 전 조회 1,563
12년 전 조회 921
12년 전 조회 1,929
12년 전 조회 1,044
12년 전 조회 1,607
12년 전 조회 2,875
12년 전 조회 2,443
12년 전 조회 1,787
12년 전 조회 6,768
12년 전 조회 2,600
12년 전 조회 820
12년 전 조회 774
12년 전 조회 1,513
12년 전 조회 2,170
12년 전 조회 989
12년 전 조회 810
12년 전 조회 1,439
12년 전 조회 1,579
12년 전 조회 801
12년 전 조회 1,237
12년 전 조회 1,513
12년 전 조회 895
12년 전 조회 1,188
12년 전 조회 3,761
12년 전 조회 1,530
12년 전 조회 1,061
12년 전 조회 957
12년 전 조회 1,320
12년 전 조회 1,559
12년 전 조회 4,345
12년 전 조회 5,677
12년 전 조회 807
12년 전 조회 2,376
12년 전 조회 6,845
12년 전 조회 2,504
12년 전 조회 9,439
12년 전 조회 637
12년 전 조회 832
12년 전 조회 1,468
12년 전 조회 1,767
12년 전 조회 1,434
12년 전 조회 2,036
🐛 버그신고