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

· 12년 전 · 1518

<?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,768
12년 전 조회 8,639
12년 전 조회 4,245
12년 전 조회 3,811
12년 전 조회 1,416
12년 전 조회 1,496
12년 전 조회 3,207
12년 전 조회 4,051
12년 전 조회 4,452
12년 전 조회 1,671
12년 전 조회 1,356
12년 전 조회 944
12년 전 조회 1,038
12년 전 조회 2,786
12년 전 조회 2,793
12년 전 조회 1,315
12년 전 조회 2,859
12년 전 조회 1,341
12년 전 조회 8,521
12년 전 조회 1,569
12년 전 조회 3,833
12년 전 조회 6,446
12년 전 조회 1,629
12년 전 조회 2,884
12년 전 조회 647
12년 전 조회 1,356
12년 전 조회 1,208
12년 전 조회 1,375
12년 전 조회 3,377
12년 전 조회 1,154
12년 전 조회 1,878
12년 전 조회 1,312
12년 전 조회 969
12년 전 조회 1,446
12년 전 조회 2,158
12년 전 조회 1,444
12년 전 조회 1,327
12년 전 조회 1,857
12년 전 조회 1.5만
12년 전 조회 1,121
12년 전 조회 934
12년 전 조회 2,776
12년 전 조회 9,021
12년 전 조회 1,337
12년 전 조회 1,229
12년 전 조회 1,694
12년 전 조회 1,201
12년 전 조회 1,346
12년 전 조회 3,613
12년 전 조회 1,519
12년 전 조회 2,193
12년 전 조회 1,211
12년 전 조회 1,360
12년 전 조회 3,085
12년 전 조회 939
12년 전 조회 1,410
12년 전 조회 1,671
12년 전 조회 2,089
12년 전 조회 2,190
12년 전 조회 2,627
12년 전 조회 1,111
12년 전 조회 1,138
12년 전 조회 4,506
12년 전 조회 1,552
12년 전 조회 1,696
12년 전 조회 5,559
12년 전 조회 3,317
12년 전 조회 2,197
12년 전 조회 1,355
12년 전 조회 3,542
12년 전 조회 2,580
12년 전 조회 2,275
12년 전 조회 1,894
12년 전 조회 1,597
12년 전 조회 1,592
12년 전 조회 1,338
12년 전 조회 2,457
12년 전 조회 1,731
12년 전 조회 1,415
12년 전 조회 2,485
12년 전 조회 1,671
12년 전 조회 2,286
12년 전 조회 3,506
12년 전 조회 1,294
12년 전 조회 2,264
12년 전 조회 1,100
12년 전 조회 1,585
12년 전 조회 2,171
12년 전 조회 1,810
12년 전 조회 4,618
12년 전 조회 2,718
12년 전 조회 2,893
12년 전 조회 1,282
12년 전 조회 8,591
12년 전 조회 3,384
12년 전 조회 1,488
12년 전 조회 1,337
12년 전 조회 2,740
12년 전 조회 5,419
12년 전 조회 3,470
🐛 버그신고