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

· 12년 전 · 1453

<?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,689
12년 전 조회 8,577
12년 전 조회 4,172
12년 전 조회 3,739
12년 전 조회 1,356
12년 전 조회 1,423
12년 전 조회 3,144
12년 전 조회 3,979
12년 전 조회 4,387
12년 전 조회 1,603
12년 전 조회 1,291
12년 전 조회 875
12년 전 조회 976
12년 전 조회 2,714
12년 전 조회 2,726
12년 전 조회 1,248
12년 전 조회 2,794
12년 전 조회 1,270
12년 전 조회 8,454
12년 전 조회 1,511
12년 전 조회 3,767
12년 전 조회 6,381
12년 전 조회 1,563
12년 전 조회 2,831
12년 전 조회 581
12년 전 조회 1,288
12년 전 조회 1,149
12년 전 조회 1,309
12년 전 조회 3,301
12년 전 조회 1,079
12년 전 조회 1,814
12년 전 조회 1,253
12년 전 조회 902
12년 전 조회 1,396
12년 전 조회 2,100
12년 전 조회 1,379
12년 전 조회 1,264
12년 전 조회 1,787
12년 전 조회 1.5만
12년 전 조회 1,060
12년 전 조회 862
12년 전 조회 2,707
12년 전 조회 8,954
12년 전 조회 1,275
12년 전 조회 1,170
12년 전 조회 1,628
12년 전 조회 1,131
12년 전 조회 1,263
12년 전 조회 3,548
12년 전 조회 1,454
12년 전 조회 2,125
12년 전 조회 1,151
12년 전 조회 1,313
12년 전 조회 3,022
12년 전 조회 871
12년 전 조회 1,336
12년 전 조회 1,602
12년 전 조회 2,029
12년 전 조회 2,134
12년 전 조회 2,558
12년 전 조회 1,054
12년 전 조회 1,076
12년 전 조회 4,439
12년 전 조회 1,493
12년 전 조회 1,632
12년 전 조회 5,491
12년 전 조회 3,265
12년 전 조회 2,135
12년 전 조회 1,295
12년 전 조회 3,485
12년 전 조회 2,514
12년 전 조회 2,223
12년 전 조회 1,840
12년 전 조회 1,535
12년 전 조회 1,526
12년 전 조회 1,281
12년 전 조회 2,399
12년 전 조회 1,669
12년 전 조회 1,347
12년 전 조회 2,432
12년 전 조회 1,611
12년 전 조회 2,225
12년 전 조회 3,440
12년 전 조회 1,226
12년 전 조회 2,201
12년 전 조회 1,038
12년 전 조회 1,524
12년 전 조회 2,112
12년 전 조회 1,743
12년 전 조회 4,560
12년 전 조회 2,657
12년 전 조회 2,824
12년 전 조회 1,227
12년 전 조회 8,537
12년 전 조회 3,334
12년 전 조회 1,434
12년 전 조회 1,281
12년 전 조회 2,681
12년 전 조회 5,360
12년 전 조회 3,410
🐛 버그신고