PHP 변수를 스크립트로 사용하는 방법입니다.

· 12년 전 · 1778

<?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) 11회 2013-03-20 13:27
|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
12년 전 조회 4,005
12년 전 조회 8,888
12년 전 조회 4,475
12년 전 조회 4,073
12년 전 조회 1,669
12년 전 조회 1,729
12년 전 조회 3,477
12년 전 조회 4,296
12년 전 조회 4,699
12년 전 조회 1,937
12년 전 조회 1,604
12년 전 조회 1,195
12년 전 조회 1,275
12년 전 조회 3,041
12년 전 조회 3,052
12년 전 조회 1,562
12년 전 조회 3,115
12년 전 조회 1,579
12년 전 조회 8,779
12년 전 조회 1,831
12년 전 조회 4,079
12년 전 조회 6,714
12년 전 조회 1,883
12년 전 조회 3,160
12년 전 조회 910
12년 전 조회 1,590
12년 전 조회 1,452
12년 전 조회 1,631
12년 전 조회 3,613
12년 전 조회 1,411
12년 전 조회 2,122
12년 전 조회 1,561
12년 전 조회 1,216
12년 전 조회 1,690
12년 전 조회 2,414
12년 전 조회 1,694
12년 전 조회 1,601
12년 전 조회 2,103
12년 전 조회 1.5만
12년 전 조회 1,371
12년 전 조회 1,180
12년 전 조회 3,019
12년 전 조회 9,259
12년 전 조회 1,559
12년 전 조회 1,503
12년 전 조회 1,943
12년 전 조회 1,445
12년 전 조회 1,598
12년 전 조회 3,887
12년 전 조회 1,779
12년 전 조회 2,446
12년 전 조회 1,471
12년 전 조회 1,614
12년 전 조회 3,332
12년 전 조회 1,184
12년 전 조회 1,640
12년 전 조회 1,907
12년 전 조회 2,336
12년 전 조회 2,453
12년 전 조회 2,874
12년 전 조회 1,342
12년 전 조회 1,370
12년 전 조회 4,756
12년 전 조회 1,798
12년 전 조회 1,945
12년 전 조회 5,799
12년 전 조회 3,540
12년 전 조회 2,443
12년 전 조회 1,608
12년 전 조회 3,785
12년 전 조회 2,812
12년 전 조회 2,514
12년 전 조회 2,163
12년 전 조회 1,838
12년 전 조회 1,844
12년 전 조회 1,589
12년 전 조회 2,716
12년 전 조회 1,989
12년 전 조회 1,665
12년 전 조회 2,746
12년 전 조회 1,936
12년 전 조회 2,547
12년 전 조회 3,753
12년 전 조회 1,543
12년 전 조회 2,495
12년 전 조회 1,352
12년 전 조회 1,839
12년 전 조회 2,424
12년 전 조회 2,052
12년 전 조회 4,863
12년 전 조회 2,967
12년 전 조회 3,137
12년 전 조회 1,508
12년 전 조회 8,829
12년 전 조회 3,661
12년 전 조회 1,749
12년 전 조회 1,599
12년 전 조회 3,004
12년 전 조회 5,668
12년 전 조회 3,713