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

· 13년 전 · 2860

<?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년 전 조회 5,193
12년 전 조회 1만
12년 전 조회 5,646
12년 전 조회 5,258
12년 전 조회 2,831
12년 전 조회 2,912
12년 전 조회 4,670
12년 전 조회 5,489
12년 전 조회 5,895
12년 전 조회 3,096
12년 전 조회 2,797
12년 전 조회 2,410
12년 전 조회 2,456
12년 전 조회 4,224
12년 전 조회 4,221
12년 전 조회 2,764
12년 전 조회 4,312
12년 전 조회 2,765
12년 전 조회 9,798
12년 전 조회 3,063
12년 전 조회 5,263
12년 전 조회 7,907
12년 전 조회 3,071
12년 전 조회 4,357
12년 전 조회 2,081
12년 전 조회 2,761
12년 전 조회 2,617
12년 전 조회 2,814
12년 전 조회 4,803
12년 전 조회 2,592
12년 전 조회 3,302
12년 전 조회 2,722
12년 전 조회 2,425
12년 전 조회 2,864
12년 전 조회 3,612
13년 전 조회 2,816
13년 전 조회 2,629
13년 전 조회 3,197
13년 전 조회 1.6만
13년 전 조회 2,449
13년 전 조회 2,244
13년 전 조회 4,060
13년 전 조회 1만
13년 전 조회 2,627
13년 전 조회 2,575
13년 전 조회 3,021
13년 전 조회 2,468
13년 전 조회 2,630
13년 전 조회 4,906
13년 전 조회 2,861
13년 전 조회 3,512
13년 전 조회 2,497
13년 전 조회 2,676
13년 전 조회 4,411
13년 전 조회 2,243
13년 전 조회 2,739
13년 전 조회 3,052
13년 전 조회 3,488
13년 전 조회 3,599
13년 전 조회 4,049
13년 전 조회 2,497
13년 전 조회 2,517
13년 전 조회 5,911
13년 전 조회 2,958
13년 전 조회 3,124
13년 전 조회 6,998
13년 전 조회 4,687
13년 전 조회 3,633
13년 전 조회 2,805
13년 전 조회 4,983
13년 전 조회 3,994
13년 전 조회 3,672
13년 전 조회 3,353
13년 전 조회 3,028
13년 전 조회 2,998
13년 전 조회 2,762
13년 전 조회 3,886
13년 전 조회 3,199
13년 전 조회 2,823
13년 전 조회 3,909
13년 전 조회 3,109
13년 전 조회 3,712
13년 전 조회 4,930
13년 전 조회 2,690
13년 전 조회 3,654
13년 전 조회 2,495
13년 전 조회 2,988
13년 전 조회 3,598
13년 전 조회 3,254
13년 전 조회 6,052
13년 전 조회 4,163
13년 전 조회 4,338
13년 전 조회 2,720
13년 전 조회 1만
13년 전 조회 4,849
13년 전 조회 2,925
13년 전 조회 2,799
13년 전 조회 4,189
13년 전 조회 6,911
13년 전 조회 4,910