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

· 12년 전 · 1449

<?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,686
12년 전 조회 8,571
12년 전 조회 4,168
12년 전 조회 3,732
12년 전 조회 1,349
12년 전 조회 1,417
12년 전 조회 3,139
12년 전 조회 3,977
12년 전 조회 4,383
12년 전 조회 1,597
12년 전 조회 1,286
12년 전 조회 871
12년 전 조회 972
12년 전 조회 2,710
12년 전 조회 2,723
12년 전 조회 1,245
12년 전 조회 2,790
12년 전 조회 1,269
12년 전 조회 8,451
12년 전 조회 1,506
12년 전 조회 3,764
12년 전 조회 6,375
12년 전 조회 1,557
12년 전 조회 2,826
12년 전 조회 577
12년 전 조회 1,284
12년 전 조회 1,147
12년 전 조회 1,304
12년 전 조회 3,298
12년 전 조회 1,073
12년 전 조회 1,808
12년 전 조회 1,245
12년 전 조회 898
12년 전 조회 1,393
12년 전 조회 2,096
12년 전 조회 1,376
12년 전 조회 1,258
12년 전 조회 1,782
12년 전 조회 1.5만
12년 전 조회 1,056
12년 전 조회 856
12년 전 조회 2,702
12년 전 조회 8,950
12년 전 조회 1,272
12년 전 조회 1,162
12년 전 조회 1,623
12년 전 조회 1,127
12년 전 조회 1,258
12년 전 조회 3,542
12년 전 조회 1,450
12년 전 조회 2,117
12년 전 조회 1,143
12년 전 조회 1,309
12년 전 조회 3,019
12년 전 조회 863
12년 전 조회 1,331
12년 전 조회 1,599
12년 전 조회 2,025
12년 전 조회 2,126
12년 전 조회 2,555
12년 전 조회 1,051
12년 전 조회 1,072
12년 전 조회 4,432
12년 전 조회 1,486
12년 전 조회 1,626
12년 전 조회 5,486
12년 전 조회 3,261
12년 전 조회 2,130
12년 전 조회 1,293
12년 전 조회 3,481
12년 전 조회 2,511
12년 전 조회 2,220
12년 전 조회 1,836
12년 전 조회 1,531
12년 전 조회 1,523
12년 전 조회 1,277
12년 전 조회 2,394
12년 전 조회 1,666
12년 전 조회 1,340
12년 전 조회 2,428
12년 전 조회 1,605
12년 전 조회 2,217
12년 전 조회 3,436
12년 전 조회 1,220
12년 전 조회 2,197
12년 전 조회 1,033
12년 전 조회 1,518
12년 전 조회 2,110
12년 전 조회 1,740
12년 전 조회 4,556
12년 전 조회 2,654
12년 전 조회 2,821
12년 전 조회 1,223
12년 전 조회 8,534
12년 전 조회 3,330
12년 전 조회 1,428
12년 전 조회 1,279
12년 전 조회 2,678
12년 전 조회 5,356
12년 전 조회 3,408
🐛 버그신고