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

· 12년 전 · 1471

<?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,706
12년 전 조회 8,591
12년 전 조회 4,190
12년 전 조회 3,756
12년 전 조회 1,375
12년 전 조회 1,442
12년 전 조회 3,163
12년 전 조회 3,997
12년 전 조회 4,405
12년 전 조회 1,619
12년 전 조회 1,305
12년 전 조회 893
12년 전 조회 992
12년 전 조회 2,730
12년 전 조회 2,744
12년 전 조회 1,269
12년 전 조회 2,812
12년 전 조회 1,288
12년 전 조회 8,473
12년 전 조회 1,525
12년 전 조회 3,782
12년 전 조회 6,397
12년 전 조회 1,578
12년 전 조회 2,845
12년 전 조회 595
12년 전 조회 1,307
12년 전 조회 1,166
12년 전 조회 1,329
12년 전 조회 3,320
12년 전 조회 1,094
12년 전 조회 1,827
12년 전 조회 1,268
12년 전 조회 917
12년 전 조회 1,403
12년 전 조회 2,118
12년 전 조회 1,398
12년 전 조회 1,278
12년 전 조회 1,802
12년 전 조회 1.5만
12년 전 조회 1,078
12년 전 조회 879
12년 전 조회 2,726
12년 전 조회 8,970
12년 전 조회 1,293
12년 전 조회 1,185
12년 전 조회 1,643
12년 전 조회 1,148
12년 전 조회 1,281
12년 전 조회 3,565
12년 전 조회 1,472
12년 전 조회 2,140
12년 전 조회 1,166
12년 전 조회 1,328
12년 전 조회 3,041
12년 전 조회 888
12년 전 조회 1,352
12년 전 조회 1,619
12년 전 조회 2,047
12년 전 조회 2,146
12년 전 조회 2,575
12년 전 조회 1,066
12년 전 조회 1,090
12년 전 조회 4,454
12년 전 조회 1,507
12년 전 조회 1,647
12년 전 조회 5,507
12년 전 조회 3,272
12년 전 조회 2,149
12년 전 조회 1,312
12년 전 조회 3,499
12년 전 조회 2,533
12년 전 조회 2,239
12년 전 조회 1,859
12년 전 조회 1,551
12년 전 조회 1,546
12년 전 조회 1,298
12년 전 조회 2,416
12년 전 조회 1,690
12년 전 조회 1,365
12년 전 조회 2,448
12년 전 조회 1,629
12년 전 조회 2,240
12년 전 조회 3,459
12년 전 조회 1,246
12년 전 조회 2,215
12년 전 조회 1,053
12년 전 조회 1,542
12년 전 조회 2,129
12년 전 조회 1,765
12년 전 조회 4,574
12년 전 조회 2,675
12년 전 조회 2,843
12년 전 조회 1,233
12년 전 조회 8,556
12년 전 조회 3,350
12년 전 조회 1,448
12년 전 조회 1,302
12년 전 조회 2,697
12년 전 조회 5,376
12년 전 조회 3,429
🐛 버그신고