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

· 12년 전 · 2414

<?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,787
12년 전 조회 9,660
12년 전 조회 5,252
12년 전 조회 4,852
12년 전 조회 2,406
12년 전 조회 2,478
12년 전 조회 4,199
12년 전 조회 5,058
12년 전 조회 5,456
12년 전 조회 2,667
12년 전 조회 2,360
12년 전 조회 1,928
12년 전 조회 2,024
12년 전 조회 3,818
12년 전 조회 3,809
12년 전 조회 2,356
12년 전 조회 3,852
12년 전 조회 2,352
12년 전 조회 9,487
12년 전 조회 2,630
12년 전 조회 4,862
12년 전 조회 7,476
12년 전 조회 2,650
12년 전 조회 3,912
12년 전 조회 1,619
12년 전 조회 2,322
12년 전 조회 2,193
12년 전 조회 2,374
12년 전 조회 4,381
12년 전 조회 2,176
12년 전 조회 2,892
12년 전 조회 2,315
12년 전 조회 1,969
12년 전 조회 2,423
12년 전 조회 3,133
12년 전 조회 2,407
12년 전 조회 2,218
12년 전 조회 2,770
12년 전 조회 1.6만
12년 전 조회 2,030
12년 전 조회 1,795
12년 전 조회 3,640
12년 전 조회 9,891
12년 전 조회 2,183
12년 전 조회 2,143
12년 전 조회 2,583
12년 전 조회 2,063
12년 전 조회 2,230
12년 전 조회 4,493
12년 전 조회 2,415
12년 전 조회 3,081
12년 전 조회 2,062
12년 전 조회 2,251
12년 전 조회 3,976
12년 전 조회 1,796
12년 전 조회 2,277
12년 전 조회 2,620
12년 전 조회 3,062
12년 전 조회 3,157
12년 전 조회 3,625
12년 전 조회 2,097
12년 전 조회 2,123
12년 전 조회 5,475
12년 전 조회 2,542
12년 전 조회 2,735
12년 전 조회 6,532
12년 전 조회 4,264
12년 전 조회 3,202
12년 전 조회 2,356
12년 전 조회 4,553
12년 전 조회 3,545
12년 전 조회 3,247
12년 전 조회 2,929
12년 전 조회 2,596
12년 전 조회 2,596
12년 전 조회 2,346
13년 전 조회 3,419
13년 전 조회 2,734
13년 전 조회 2,413
13년 전 조회 3,481
13년 전 조회 2,706
13년 전 조회 3,265
13년 전 조회 4,483
13년 전 조회 2,250
13년 전 조회 3,251
13년 전 조회 2,092
13년 전 조회 2,548
13년 전 조회 3,220
13년 전 조회 2,832
13년 전 조회 5,633
13년 전 조회 3,736
13년 전 조회 3,913
13년 전 조회 2,309
13년 전 조회 9,616
13년 전 조회 4,442
13년 전 조회 2,494
13년 전 조회 2,338
13년 전 조회 3,784
13년 전 조회 6,495
13년 전 조회 4,495