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

· 13년 전 · 2827

<?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,174
12년 전 조회 1만
12년 전 조회 5,631
12년 전 조회 5,235
12년 전 조회 2,816
12년 전 조회 2,895
12년 전 조회 4,643
12년 전 조회 5,460
12년 전 조회 5,864
12년 전 조회 3,066
12년 전 조회 2,773
12년 전 조회 2,399
12년 전 조회 2,437
12년 전 조회 4,205
12년 전 조회 4,200
12년 전 조회 2,748
12년 전 조회 4,291
12년 전 조회 2,740
12년 전 조회 9,787
12년 전 조회 3,039
12년 전 조회 5,245
12년 전 조회 7,880
12년 전 조회 3,049
12년 전 조회 4,338
12년 전 조회 2,057
12년 전 조회 2,734
12년 전 조회 2,596
12년 전 조회 2,781
12년 전 조회 4,774
12년 전 조회 2,570
12년 전 조회 3,274
12년 전 조회 2,703
12년 전 조회 2,387
12년 전 조회 2,845
12년 전 조회 3,583
12년 전 조회 2,794
12년 전 조회 2,607
12년 전 조회 3,164
12년 전 조회 1.6만
13년 전 조회 2,424
13년 전 조회 2,229
13년 전 조회 4,034
13년 전 조회 1만
13년 전 조회 2,607
13년 전 조회 2,549
13년 전 조회 2,999
13년 전 조회 2,438
13년 전 조회 2,611
13년 전 조회 4,879
13년 전 조회 2,828
13년 전 조회 3,482
13년 전 조회 2,474
13년 전 조회 2,654
13년 전 조회 4,383
13년 전 조회 2,221
13년 전 조회 2,722
13년 전 조회 3,027
13년 전 조회 3,469
13년 전 조회 3,574
13년 전 조회 4,029
13년 전 조회 2,484
13년 전 조회 2,493
13년 전 조회 5,884
13년 전 조회 2,929
13년 전 조회 3,101
13년 전 조회 6,975
13년 전 조회 4,672
13년 전 조회 3,605
13년 전 조회 2,778
13년 전 조회 4,957
13년 전 조회 3,966
13년 전 조회 3,652
13년 전 조회 3,327
13년 전 조회 3,007
13년 전 조회 2,980
13년 전 조회 2,743
13년 전 조회 3,862
13년 전 조회 3,170
13년 전 조회 2,798
13년 전 조회 3,885
13년 전 조회 3,091
13년 전 조회 3,687
13년 전 조회 4,910
13년 전 조회 2,662
13년 전 조회 3,628
13년 전 조회 2,473
13년 전 조회 2,968
13년 전 조회 3,576
13년 전 조회 3,225
13년 전 조회 6,021
13년 전 조회 4,136
13년 전 조회 4,295
13년 전 조회 2,695
13년 전 조회 1만
13년 전 조회 4,825
13년 전 조회 2,895
13년 전 조회 2,776
13년 전 조회 4,164
13년 전 조회 6,888
13년 전 조회 4,886