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

· 13년 전 · 2874

<?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,204
12년 전 조회 1만
12년 전 조회 5,658
12년 전 조회 5,270
12년 전 조회 2,844
12년 전 조회 2,925
12년 전 조회 4,683
12년 전 조회 5,498
12년 전 조회 5,906
12년 전 조회 3,106
12년 전 조회 2,811
12년 전 조회 2,423
12년 전 조회 2,473
12년 전 조회 4,234
12년 전 조회 4,241
12년 전 조회 2,767
12년 전 조회 4,321
12년 전 조회 2,776
12년 전 조회 9,808
12년 전 조회 3,072
12년 전 조회 5,274
12년 전 조회 7,918
12년 전 조회 3,092
12년 전 조회 4,370
12년 전 조회 2,090
12년 전 조회 2,769
12년 전 조회 2,634
12년 전 조회 2,831
12년 전 조회 4,818
12년 전 조회 2,607
12년 전 조회 3,316
12년 전 조회 2,740
12년 전 조회 2,436
12년 전 조회 2,872
12년 전 조회 3,626
13년 전 조회 2,829
13년 전 조회 2,644
13년 전 조회 3,207
13년 전 조회 1.6만
13년 전 조회 2,464
13년 전 조회 2,264
13년 전 조회 4,076
13년 전 조회 1만
13년 전 조회 2,642
13년 전 조회 2,595
13년 전 조회 3,040
13년 전 조회 2,490
13년 전 조회 2,645
13년 전 조회 4,925
13년 전 조회 2,875
13년 전 조회 3,529
13년 전 조회 2,508
13년 전 조회 2,691
13년 전 조회 4,423
13년 전 조회 2,259
13년 전 조회 2,763
13년 전 조회 3,070
13년 전 조회 3,502
13년 전 조회 3,610
13년 전 조회 4,064
13년 전 조회 2,513
13년 전 조회 2,533
13년 전 조회 5,930
13년 전 조회 2,971
13년 전 조회 3,139
13년 전 조회 7,017
13년 전 조회 4,696
13년 전 조회 3,649
13년 전 조회 2,819
13년 전 조회 4,994
13년 전 조회 4,006
13년 전 조회 3,686
13년 전 조회 3,367
13년 전 조회 3,040
13년 전 조회 3,018
13년 전 조회 2,778
13년 전 조회 3,906
13년 전 조회 3,215
13년 전 조회 2,835
13년 전 조회 3,923
13년 전 조회 3,126
13년 전 조회 3,726
13년 전 조회 4,948
13년 전 조회 2,708
13년 전 조회 3,676
13년 전 조회 2,509
13년 전 조회 2,997
13년 전 조회 3,613
13년 전 조회 3,266
13년 전 조회 6,070
13년 전 조회 4,183
13년 전 조회 4,355
13년 전 조회 2,734
13년 전 조회 1만
13년 전 조회 4,867
13년 전 조회 2,941
13년 전 조회 2,817
13년 전 조회 4,211
13년 전 조회 6,931
13년 전 조회 4,926