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

· 12년 전 · 2687

<?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,019
12년 전 조회 9,881
12년 전 조회 5,496
12년 전 조회 5,082
12년 전 조회 2,663
12년 전 조회 2,756
12년 전 조회 4,483
12년 전 조회 5,318
12년 전 조회 5,718
12년 전 조회 2,930
12년 전 조회 2,642
12년 전 조회 2,269
12년 전 조회 2,301
12년 전 조회 4,064
12년 전 조회 4,062
12년 전 조회 2,617
12년 전 조회 4,146
12년 전 조회 2,616
12년 전 조회 9,712
12년 전 조회 2,906
12년 전 조회 5,101
12년 전 조회 7,715
12년 전 조회 2,894
12년 전 조회 4,214
12년 전 조회 1,919
12년 전 조회 2,597
12년 전 조회 2,465
12년 전 조회 2,645
12년 전 조회 4,636
12년 전 조회 2,436
12년 전 조회 3,152
12년 전 조회 2,576
12년 전 조회 2,253
12년 전 조회 2,697
12년 전 조회 3,437
12년 전 조회 2,671
12년 전 조회 2,475
12년 전 조회 3,016
12년 전 조회 1.6만
12년 전 조회 2,296
12년 전 조회 2,111
12년 전 조회 3,895
12년 전 조회 1만
12년 전 조회 2,464
12년 전 조회 2,402
12년 전 조회 2,847
12년 전 조회 2,306
12년 전 조회 2,463
12년 전 조회 4,735
12년 전 조회 2,688
12년 전 조회 3,338
12년 전 조회 2,345
12년 전 조회 2,506
12년 전 조회 4,215
12년 전 조회 2,091
12년 전 조회 2,566
12년 전 조회 2,884
12년 전 조회 3,333
13년 전 조회 3,446
13년 전 조회 3,886
13년 전 조회 2,339
13년 전 조회 2,346
13년 전 조회 5,734
13년 전 조회 2,795
13년 전 조회 2,957
13년 전 조회 6,836
13년 전 조회 4,540
13년 전 조회 3,455
13년 전 조회 2,655
13년 전 조회 4,830
13년 전 조회 3,809
13년 전 조회 3,509
13년 전 조회 3,171
13년 전 조회 2,853
13년 전 조회 2,848
13년 전 조회 2,577
13년 전 조회 3,714
13년 전 조회 3,010
13년 전 조회 2,643
13년 전 조회 3,714
13년 전 조회 2,933
13년 전 조회 3,522
13년 전 조회 4,759
13년 전 조회 2,529
13년 전 조회 3,501
13년 전 조회 2,343
13년 전 조회 2,803
13년 전 조회 3,429
13년 전 조회 3,073
13년 전 조회 5,872
13년 전 조회 3,982
13년 전 조회 4,167
13년 전 조회 2,524
13년 전 조회 9,857
13년 전 조회 4,685
13년 전 조회 2,752
13년 전 조회 2,621
13년 전 조회 4,009
13년 전 조회 6,734
13년 전 조회 4,746