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

· 12년 전 · 1755

<?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년 전 조회 3,994
12년 전 조회 8,873
12년 전 조회 4,456
12년 전 조회 4,054
12년 전 조회 1,664
12년 전 조회 1,720
12년 전 조회 3,454
12년 전 조회 4,281
12년 전 조회 4,682
12년 전 조회 1,918
12년 전 조회 1,590
12년 전 조회 1,185
12년 전 조회 1,267
12년 전 조회 3,012
12년 전 조회 3,034
12년 전 조회 1,548
12년 전 조회 3,102
12년 전 조회 1,566
12년 전 조회 8,760
12년 전 조회 1,815
12년 전 조회 4,063
12년 전 조회 6,692
12년 전 조회 1,864
12년 전 조회 3,148
12년 전 조회 894
12년 전 조회 1,581
12년 전 조회 1,435
12년 전 조회 1,608
12년 전 조회 3,596
12년 전 조회 1,388
12년 전 조회 2,100
12년 전 조회 1,548
12년 전 조회 1,202
12년 전 조회 1,680
12년 전 조회 2,403
12년 전 조회 1,676
12년 전 조회 1,581
12년 전 조회 2,088
12년 전 조회 1.5만
12년 전 조회 1,352
12년 전 조회 1,168
12년 전 조회 3,004
12년 전 조회 9,243
12년 전 조회 1,552
12년 전 조회 1,483
12년 전 조회 1,925
12년 전 조회 1,428
12년 전 조회 1,579
12년 전 조회 3,858
12년 전 조회 1,756
12년 전 조회 2,425
12년 전 조회 1,457
12년 전 조회 1,597
12년 전 조회 3,311
12년 전 조회 1,171
12년 전 조회 1,624
12년 전 조회 1,893
12년 전 조회 2,314
12년 전 조회 2,438
12년 전 조회 2,855
12년 전 조회 1,323
12년 전 조회 1,347
12년 전 조회 4,745
12년 전 조회 1,776
12년 전 조회 1,925
12년 전 조회 5,783
12년 전 조회 3,526
12년 전 조회 2,430
12년 전 조회 1,590
12년 전 조회 3,774
12년 전 조회 2,799
12년 전 조회 2,499
12년 전 조회 2,140
12년 전 조회 1,820
12년 전 조회 1,821
12년 전 조회 1,578
12년 전 조회 2,698
12년 전 조회 1,973
12년 전 조회 1,647
12년 전 조회 2,729
12년 전 조회 1,916
12년 전 조회 2,536
12년 전 조회 3,739
12년 전 조회 1,526
12년 전 조회 2,482
12년 전 조회 1,338
12년 전 조회 1,831
12년 전 조회 2,407
12년 전 조회 2,034
12년 전 조회 4,843
12년 전 조회 2,950
12년 전 조회 3,123
12년 전 조회 1,495
12년 전 조회 8,808
12년 전 조회 3,639
12년 전 조회 1,733
12년 전 조회 1,586
12년 전 조회 2,983
12년 전 조회 5,653
12년 전 조회 3,696