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

· 13년 전 · 2821

<?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,168
12년 전 조회 1만
12년 전 조회 5,628
12년 전 조회 5,230
12년 전 조회 2,808
12년 전 조회 2,888
12년 전 조회 4,641
12년 전 조회 5,456
12년 전 조회 5,861
12년 전 조회 3,064
12년 전 조회 2,766
12년 전 조회 2,395
12년 전 조회 2,436
12년 전 조회 4,198
12년 전 조회 4,198
12년 전 조회 2,740
12년 전 조회 4,288
12년 전 조회 2,737
12년 전 조회 9,783
12년 전 조회 3,035
12년 전 조회 5,240
12년 전 조회 7,877
12년 전 조회 3,044
12년 전 조회 4,335
12년 전 조회 2,052
12년 전 조회 2,732
12년 전 조회 2,592
12년 전 조회 2,775
12년 전 조회 4,772
12년 전 조회 2,565
12년 전 조회 3,272
12년 전 조회 2,699
12년 전 조회 2,381
12년 전 조회 2,841
12년 전 조회 3,579
12년 전 조회 2,794
12년 전 조회 2,604
12년 전 조회 3,158
12년 전 조회 1.6만
12년 전 조회 2,420
13년 전 조회 2,222
13년 전 조회 4,028
13년 전 조회 1만
13년 전 조회 2,597
13년 전 조회 2,543
13년 전 조회 2,986
13년 전 조회 2,433
13년 전 조회 2,608
13년 전 조회 4,876
13년 전 조회 2,822
13년 전 조회 3,478
13년 전 조회 2,471
13년 전 조회 2,653
13년 전 조회 4,381
13년 전 조회 2,216
13년 전 조회 2,721
13년 전 조회 3,021
13년 전 조회 3,465
13년 전 조회 3,572
13년 전 조회 4,026
13년 전 조회 2,482
13년 전 조회 2,486
13년 전 조회 5,880
13년 전 조회 2,928
13년 전 조회 3,094
13년 전 조회 6,973
13년 전 조회 4,668
13년 전 조회 3,597
13년 전 조회 2,773
13년 전 조회 4,954
13년 전 조회 3,963
13년 전 조회 3,645
13년 전 조회 3,327
13년 전 조회 3,005
13년 전 조회 2,976
13년 전 조회 2,736
13년 전 조회 3,857
13년 전 조회 3,166
13년 전 조회 2,794
13년 전 조회 3,878
13년 전 조회 3,083
13년 전 조회 3,681
13년 전 조회 4,904
13년 전 조회 2,659
13년 전 조회 3,624
13년 전 조회 2,468
13년 전 조회 2,961
13년 전 조회 3,572
13년 전 조회 3,219
13년 전 조회 6,019
13년 전 조회 4,131
13년 전 조회 4,292
13년 전 조회 2,688
13년 전 조회 9,999
13년 전 조회 4,821
13년 전 조회 2,890
13년 전 조회 2,766
13년 전 조회 4,158
13년 전 조회 6,884
13년 전 조회 4,882