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

· 12년 전 · 1487

<?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년 전 조회 3,729
12년 전 조회 8,611
12년 전 조회 4,209
12년 전 조회 3,777
12년 전 조회 1,393
12년 전 조회 1,465
12년 전 조회 3,180
12년 전 조회 4,017
12년 전 조회 4,416
12년 전 조회 1,639
12년 전 조회 1,324
12년 전 조회 907
12년 전 조회 1,012
12년 전 조회 2,749
12년 전 조회 2,762
12년 전 조회 1,283
12년 전 조회 2,824
12년 전 조회 1,309
12년 전 조회 8,487
12년 전 조회 1,537
12년 전 조회 3,800
12년 전 조회 6,414
12년 전 조회 1,597
12년 전 조회 2,862
12년 전 조회 614
12년 전 조회 1,327
12년 전 조회 1,183
12년 전 조회 1,347
12년 전 조회 3,343
12년 전 조회 1,116
12년 전 조회 1,848
12년 전 조회 1,285
12년 전 조회 931
12년 전 조회 1,420
12년 전 조회 2,129
12년 전 조회 1,415
12년 전 조회 1,296
12년 전 조회 1,819
12년 전 조회 1.5만
12년 전 조회 1,094
12년 전 조회 900
12년 전 조회 2,739
12년 전 조회 8,992
12년 전 조회 1,309
12년 전 조회 1,197
12년 전 조회 1,662
12년 전 조회 1,164
12년 전 조회 1,299
12년 전 조회 3,581
12년 전 조회 1,488
12년 전 조회 2,155
12년 전 조회 1,179
12년 전 조회 1,341
12년 전 조회 3,059
12년 전 조회 904
12년 전 조회 1,374
12년 전 조회 1,639
12년 전 조회 2,064
12년 전 조회 2,162
12년 전 조회 2,594
12년 전 조회 1,081
12년 전 조회 1,108
12년 전 조회 4,472
12년 전 조회 1,523
12년 전 조회 1,666
12년 전 조회 5,528
12년 전 조회 3,288
12년 전 조회 2,167
12년 전 조회 1,325
12년 전 조회 3,512
12년 전 조회 2,548
12년 전 조회 2,253
12년 전 조회 1,869
12년 전 조회 1,566
12년 전 조회 1,562
12년 전 조회 1,313
12년 전 조회 2,431
12년 전 조회 1,702
12년 전 조회 1,383
12년 전 조회 2,465
12년 전 조회 1,643
12년 전 조회 2,254
12년 전 조회 3,475
12년 전 조회 1,263
12년 전 조회 2,231
12년 전 조회 1,065
12년 전 조회 1,555
12년 전 조회 2,145
12년 전 조회 1,788
12년 전 조회 4,589
12년 전 조회 2,689
12년 전 조회 2,864
12년 전 조회 1,254
12년 전 조회 8,564
12년 전 조회 3,361
12년 전 조회 1,468
12년 전 조회 1,314
12년 전 조회 2,716
12년 전 조회 5,391
12년 전 조회 3,442
🐛 버그신고