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

· 12년 전 · 2669

<?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,001
12년 전 조회 9,866
12년 전 조회 5,480
12년 전 조회 5,071
12년 전 조회 2,647
12년 전 조회 2,740
12년 전 조회 4,472
12년 전 조회 5,309
12년 전 조회 5,704
12년 전 조회 2,917
12년 전 조회 2,624
12년 전 조회 2,257
12년 전 조회 2,282
12년 전 조회 4,054
12년 전 조회 4,041
12년 전 조회 2,601
12년 전 조회 4,133
12년 전 조회 2,595
12년 전 조회 9,707
12년 전 조회 2,894
12년 전 조회 5,085
12년 전 조회 7,701
12년 전 조회 2,884
12년 전 조회 4,201
12년 전 조회 1,911
12년 전 조회 2,581
12년 전 조회 2,451
12년 전 조회 2,626
12년 전 조회 4,626
12년 전 조회 2,422
12년 전 조회 3,138
12년 전 조회 2,563
12년 전 조회 2,237
12년 전 조회 2,682
12년 전 조회 3,424
12년 전 조회 2,662
12년 전 조회 2,462
12년 전 조회 3,001
12년 전 조회 1.6만
12년 전 조회 2,282
12년 전 조회 2,097
12년 전 조회 3,883
12년 전 조회 1만
12년 전 조회 2,446
12년 전 조회 2,390
12년 전 조회 2,830
12년 전 조회 2,295
12년 전 조회 2,449
12년 전 조회 4,724
12년 전 조회 2,670
12년 전 조회 3,325
12년 전 조회 2,330
12년 전 조회 2,489
12년 전 조회 4,205
12년 전 조회 2,078
12년 전 조회 2,554
12년 전 조회 2,874
12년 전 조회 3,319
13년 전 조회 3,423
13년 전 조회 3,877
13년 전 조회 2,324
13년 전 조회 2,333
13년 전 조회 5,726
13년 전 조회 2,782
13년 전 조회 2,941
13년 전 조회 6,818
13년 전 조회 4,528
13년 전 조회 3,444
13년 전 조회 2,639
13년 전 조회 4,812
13년 전 조회 3,796
13년 전 조회 3,492
13년 전 조회 3,157
13년 전 조회 2,844
13년 전 조회 2,833
13년 전 조회 2,561
13년 전 조회 3,693
13년 전 조회 2,991
13년 전 조회 2,624
13년 전 조회 3,700
13년 전 조회 2,920
13년 전 조회 3,510
13년 전 조회 4,744
13년 전 조회 2,512
13년 전 조회 3,485
13년 전 조회 2,329
13년 전 조회 2,787
13년 전 조회 3,421
13년 전 조회 3,062
13년 전 조회 5,858
13년 전 조회 3,968
13년 전 조회 4,157
13년 전 조회 2,509
13년 전 조회 9,846
13년 전 조회 4,673
13년 전 조회 2,736
13년 전 조회 2,607
13년 전 조회 3,994
13년 전 조회 6,715
13년 전 조회 4,729