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

· 12년 전 · 2725

<?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,055
12년 전 조회 9,918
12년 전 조회 5,527
12년 전 조회 5,114
12년 전 조회 2,692
12년 전 조회 2,779
12년 전 조회 4,517
12년 전 조회 5,347
12년 전 조회 5,752
12년 전 조회 2,952
12년 전 조회 2,664
12년 전 조회 2,296
12년 전 조회 2,321
12년 전 조회 4,091
12년 전 조회 4,098
12년 전 조회 2,639
12년 전 조회 4,179
12년 전 조회 2,635
12년 전 조회 9,725
12년 전 조회 2,938
12년 전 조회 5,127
12년 전 조회 7,747
12년 전 조회 2,929
12년 전 조회 4,236
12년 전 조회 1,945
12년 전 조회 2,625
12년 전 조회 2,489
12년 전 조회 2,662
12년 전 조회 4,660
12년 전 조회 2,460
12년 전 조회 3,173
12년 전 조회 2,606
12년 전 조회 2,280
12년 전 조회 2,725
12년 전 조회 3,462
12년 전 조회 2,696
12년 전 조회 2,497
12년 전 조회 3,054
12년 전 조회 1.6만
12년 전 조회 2,325
12년 전 조회 2,132
12년 전 조회 3,927
12년 전 조회 1만
12년 전 조회 2,489
12년 전 조회 2,438
12년 전 조회 2,883
12년 전 조회 2,333
12년 전 조회 2,508
12년 전 조회 4,770
12년 전 조회 2,726
12년 전 조회 3,372
12년 전 조회 2,367
12년 전 조회 2,536
12년 전 조회 4,251
12년 전 조회 2,116
12년 전 조회 2,600
13년 전 조회 2,909
13년 전 조회 3,361
13년 전 조회 3,468
13년 전 조회 3,906
13년 전 조회 2,367
13년 전 조회 2,384
13년 전 조회 5,767
13년 전 조회 2,821
13년 전 조회 2,991
13년 전 조회 6,861
13년 전 조회 4,565
13년 전 조회 3,475
13년 전 조회 2,680
13년 전 조회 4,850
13년 전 조회 3,836
13년 전 조회 3,526
13년 전 조회 3,199
13년 전 조회 2,886
13년 전 조회 2,869
13년 전 조회 2,615
13년 전 조회 3,752
13년 전 조회 3,044
13년 전 조회 2,673
13년 전 조회 3,764
13년 전 조회 2,982
13년 전 조회 3,558
13년 전 조회 4,790
13년 전 조회 2,557
13년 전 조회 3,531
13년 전 조회 2,372
13년 전 조회 2,834
13년 전 조회 3,462
13년 전 조회 3,107
13년 전 조회 5,911
13년 전 조회 4,010
13년 전 조회 4,195
13년 전 조회 2,562
13년 전 조회 9,896
13년 전 조회 4,719
13년 전 조회 2,783
13년 전 조회 2,652
13년 전 조회 4,046
13년 전 조회 6,765
13년 전 조회 4,773