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

· 12년 전 · 2486

<?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,831
12년 전 조회 9,701
12년 전 조회 5,300
12년 전 조회 4,885
12년 전 조회 2,484
12년 전 조회 2,555
12년 전 조회 4,283
12년 전 조회 5,127
12년 전 조회 5,519
12년 전 조회 2,745
12년 전 조회 2,440
12년 전 조회 2,029
12년 전 조회 2,108
12년 전 조회 3,887
12년 전 조회 3,866
12년 전 조회 2,414
12년 전 조회 3,928
12년 전 조회 2,434
12년 전 조회 9,598
12년 전 조회 2,691
12년 전 조회 4,925
12년 전 조회 7,522
12년 전 조회 2,704
12년 전 조회 4,004
12년 전 조회 1,706
12년 전 조회 2,393
12년 전 조회 2,268
12년 전 조회 2,445
12년 전 조회 4,457
12년 전 조회 2,230
12년 전 조회 2,971
12년 전 조회 2,382
12년 전 조회 2,052
12년 전 조회 2,492
12년 전 조회 3,209
12년 전 조회 2,482
12년 전 조회 2,294
12년 전 조회 2,815
12년 전 조회 1.6만
12년 전 조회 2,090
12년 전 조회 1,895
12년 전 조회 3,705
12년 전 조회 9,957
12년 전 조회 2,255
12년 전 조회 2,211
12년 전 조회 2,659
12년 전 조회 2,120
12년 전 조회 2,276
12년 전 조회 4,545
12년 전 조회 2,487
12년 전 조회 3,152
12년 전 조회 2,134
12년 전 조회 2,316
12년 전 조회 4,031
12년 전 조회 1,877
12년 전 조회 2,358
12년 전 조회 2,694
12년 전 조회 3,126
12년 전 조회 3,229
12년 전 조회 3,694
12년 전 조회 2,144
12년 전 조회 2,183
12년 전 조회 5,553
12년 전 조회 2,605
12년 전 조회 2,786
12년 전 조회 6,616
12년 전 조회 4,348
12년 전 조회 3,275
12년 전 조회 2,448
12년 전 조회 4,631
12년 전 조회 3,615
13년 전 조회 3,322
13년 전 조회 2,970
13년 전 조회 2,651
13년 전 조회 2,669
13년 전 조회 2,398
13년 전 조회 3,495
13년 전 조회 2,817
13년 전 조회 2,479
13년 전 조회 3,528
13년 전 조회 2,748
13년 전 조회 3,329
13년 전 조회 4,566
13년 전 조회 2,328
13년 전 조회 3,332
13년 전 조회 2,158
13년 전 조회 2,620
13년 전 조회 3,260
13년 전 조회 2,896
13년 전 조회 5,683
13년 전 조회 3,793
13년 전 조회 4,003
13년 전 조회 2,355
13년 전 조회 9,667
13년 전 조회 4,503
13년 전 조회 2,557
13년 전 조회 2,427
13년 전 조회 3,829
13년 전 조회 6,547
13년 전 조회 4,552