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

· 13년 전 · 2877

<?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,211
12년 전 조회 1만
12년 전 조회 5,663
12년 전 조회 5,270
12년 전 조회 2,849
12년 전 조회 2,931
12년 전 조회 4,692
12년 전 조회 5,503
12년 전 조회 5,916
12년 전 조회 3,109
12년 전 조회 2,815
12년 전 조회 2,426
12년 전 조회 2,476
12년 전 조회 4,239
12년 전 조회 4,246
12년 전 조회 2,777
12년 전 조회 4,325
12년 전 조회 2,782
12년 전 조회 9,811
12년 전 조회 3,075
12년 전 조회 5,281
12년 전 조회 7,925
12년 전 조회 3,098
12년 전 조회 4,374
12년 전 조회 2,097
12년 전 조회 2,778
12년 전 조회 2,640
12년 전 조회 2,839
12년 전 조회 4,821
12년 전 조회 2,611
12년 전 조회 3,320
12년 전 조회 2,744
12년 전 조회 2,442
12년 전 조회 2,878
12년 전 조회 3,631
13년 전 조회 2,832
13년 전 조회 2,648
13년 전 조회 3,212
13년 전 조회 1.6만
13년 전 조회 2,474
13년 전 조회 2,267
13년 전 조회 4,083
13년 전 조회 1만
13년 전 조회 2,649
13년 전 조회 2,605
13년 전 조회 3,043
13년 전 조회 2,495
13년 전 조회 2,653
13년 전 조회 4,931
13년 전 조회 2,878
13년 전 조회 3,534
13년 전 조회 2,515
13년 전 조회 2,698
13년 전 조회 4,430
13년 전 조회 2,266
13년 전 조회 2,768
13년 전 조회 3,076
13년 전 조회 3,511
13년 전 조회 3,615
13년 전 조회 4,070
13년 전 조회 2,519
13년 전 조회 2,537
13년 전 조회 5,934
13년 전 조회 2,981
13년 전 조회 3,148
13년 전 조회 7,023
13년 전 조회 4,697
13년 전 조회 3,652
13년 전 조회 2,827
13년 전 조회 5,006
13년 전 조회 4,015
13년 전 조회 3,690
13년 전 조회 3,374
13년 전 조회 3,043
13년 전 조회 3,029
13년 전 조회 2,787
13년 전 조회 3,914
13년 전 조회 3,223
13년 전 조회 2,839
13년 전 조회 3,928
13년 전 조회 3,135
13년 전 조회 3,735
13년 전 조회 4,953
13년 전 조회 2,714
13년 전 조회 3,684
13년 전 조회 2,515
13년 전 조회 3,006
13년 전 조회 3,619
13년 전 조회 3,271
13년 전 조회 6,079
13년 전 조회 4,191
13년 전 조회 4,359
13년 전 조회 2,745
13년 전 조회 1만
13년 전 조회 4,876
13년 전 조회 2,955
13년 전 조회 2,823
13년 전 조회 4,221
13년 전 조회 6,944
13년 전 조회 4,938