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

· 12년 전 · 2130

<?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년 전 조회 4,462
12년 전 조회 9,360
12년 전 조회 4,903
12년 전 조회 4,535
12년 전 조회 2,119
12년 전 조회 2,162
12년 전 조회 3,923
12년 전 조회 4,753
12년 전 조회 5,139
12년 전 조회 2,389
12년 전 조회 2,053
12년 전 조회 1,631
12년 전 조회 1,701
12년 전 조회 3,506
12년 전 조회 3,485
12년 전 조회 2,014
12년 전 조회 3,541
12년 전 조회 2,031
12년 전 조회 9,304
12년 전 조회 2,277
12년 전 조회 4,502
12년 전 조회 7,161
12년 전 조회 2,315
12년 전 조회 3,581
12년 전 조회 1,314
12년 전 조회 2,017
12년 전 조회 1,896
12년 전 조회 2,069
12년 전 조회 4,054
12년 전 조회 1,826
12년 전 조회 2,575
12년 전 조회 2,020
12년 전 조회 1,651
12년 전 조회 2,078
12년 전 조회 2,827
12년 전 조회 2,111
12년 전 조회 1,949
12년 전 조회 2,469
12년 전 조회 1.5만
12년 전 조회 1,730
12년 전 조회 1,513
12년 전 조회 3,361
12년 전 조회 9,592
12년 전 조회 1,903
12년 전 조회 1,859
12년 전 조회 2,299
12년 전 조회 1,776
12년 전 조회 1,960
12년 전 조회 4,229
12년 전 조회 2,131
12년 전 조회 2,802
12년 전 조회 1,794
12년 전 조회 1,965
12년 전 조회 3,673
12년 전 조회 1,513
12년 전 조회 1,998
12년 전 조회 2,329
12년 전 조회 2,755
12년 전 조회 2,862
12년 전 조회 3,305
12년 전 조회 1,764
12년 전 조회 1,810
12년 전 조회 5,185
12년 전 조회 2,240
12년 전 조회 2,420
12년 전 조회 6,250
12년 전 조회 3,938
12년 전 조회 2,911
12년 전 조회 2,054
12년 전 조회 4,229
12년 전 조회 3,243
12년 전 조회 2,913
12년 전 조회 2,600
12년 전 조회 2,277
12년 전 조회 2,291
12년 전 조회 2,030
12년 전 조회 3,110
12년 전 조회 2,430
12년 전 조회 2,125
12년 전 조회 3,195
12년 전 조회 2,384
12년 전 조회 2,967
12년 전 조회 4,189
12년 전 조회 1,942
12년 전 조회 2,921
12년 전 조회 1,785
12년 전 조회 2,265
12년 전 조회 2,903
12년 전 조회 2,505
12년 전 조회 5,315
12년 전 조회 3,432
12년 전 조회 3,629
12년 전 조회 1,990
12년 전 조회 9,315
12년 전 조회 4,134
12년 전 조회 2,192
12년 전 조회 2,053
12년 전 조회 3,485
12년 전 조회 6,172
12년 전 조회 4,187