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

· 12년 전 · 1621

<?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,859
12년 전 조회 8,751
12년 전 조회 4,334
12년 전 조회 3,909
12년 전 조회 1,511
12년 전 조회 1,588
12년 전 조회 3,304
12년 전 조회 4,143
12년 전 조회 4,548
12년 전 조회 1,770
12년 전 조회 1,451
12년 전 조회 1,046
12년 전 조회 1,146
12년 전 조회 2,886
12년 전 조회 2,883
12년 전 조회 1,410
12년 전 조회 2,959
12년 전 조회 1,440
12년 전 조회 8,615
12년 전 조회 1,678
12년 전 조회 3,934
12년 전 조회 6,540
12년 전 조회 1,729
12년 전 조회 2,995
12년 전 조회 753
12년 전 조회 1,456
12년 전 조회 1,316
12년 전 조회 1,470
12년 전 조회 3,468
12년 전 조회 1,248
12년 전 조회 1,981
12년 전 조회 1,418
12년 전 조회 1,073
12년 전 조회 1,552
12년 전 조회 2,264
12년 전 조회 1,542
12년 전 조회 1,437
12년 전 조회 1,959
12년 전 조회 1.5만
12년 전 조회 1,233
12년 전 조회 1,039
12년 전 조회 2,880
12년 전 조회 9,112
12년 전 조회 1,427
12년 전 조회 1,335
12년 전 조회 1,793
12년 전 조회 1,301
12년 전 조회 1,439
12년 전 조회 3,713
12년 전 조회 1,622
12년 전 조회 2,291
12년 전 조회 1,307
12년 전 조회 1,464
12년 전 조회 3,194
12년 전 조회 1,031
12년 전 조회 1,503
12년 전 조회 1,764
12년 전 조회 2,187
12년 전 조회 2,295
12년 전 조회 2,724
12년 전 조회 1,210
12년 전 조회 1,235
12년 전 조회 4,607
12년 전 조회 1,648
12년 전 조회 1,797
12년 전 조회 5,664
12년 전 조회 3,414
12년 전 조회 2,312
12년 전 조회 1,455
12년 전 조회 3,645
12년 전 조회 2,665
12년 전 조회 2,378
12년 전 조회 1,995
12년 전 조회 1,699
12년 전 조회 1,696
12년 전 조회 1,457
12년 전 조회 2,556
12년 전 조회 1,838
12년 전 조회 1,511
12년 전 조회 2,592
12년 전 조회 1,761
12년 전 조회 2,394
12년 전 조회 3,613
12년 전 조회 1,391
12년 전 조회 2,356
12년 전 조회 1,206
12년 전 조회 1,695
12년 전 조회 2,274
12년 전 조회 1,905
12년 전 조회 4,730
12년 전 조회 2,820
12년 전 조회 2,994
12년 전 조회 1,370
12년 전 조회 8,702
12년 전 조회 3,501
12년 전 조회 1,587
12년 전 조회 1,445
12년 전 조회 2,847
12년 전 조회 5,528
12년 전 조회 3,570
🐛 버그신고