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

· 12년 전 · 1458

<?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,692
12년 전 조회 8,580
12년 전 조회 4,174
12년 전 조회 3,742
12년 전 조회 1,361
12년 전 조회 1,427
12년 전 조회 3,148
12년 전 조회 3,984
12년 전 조회 4,392
12년 전 조회 1,605
12년 전 조회 1,297
12년 전 조회 879
12년 전 조회 979
12년 전 조회 2,718
12년 전 조회 2,729
12년 전 조회 1,252
12년 전 조회 2,799
12년 전 조회 1,274
12년 전 조회 8,459
12년 전 조회 1,513
12년 전 조회 3,768
12년 전 조회 6,383
12년 전 조회 1,566
12년 전 조회 2,833
12년 전 조회 583
12년 전 조회 1,292
12년 전 조회 1,152
12년 전 조회 1,314
12년 전 조회 3,304
12년 전 조회 1,084
12년 전 조회 1,816
12년 전 조회 1,256
12년 전 조회 907
12년 전 조회 1,397
12년 전 조회 2,103
12년 전 조회 1,382
12년 전 조회 1,266
12년 전 조회 1,791
12년 전 조회 1.5만
12년 전 조회 1,062
12년 전 조회 867
12년 전 조회 2,711
12년 전 조회 8,956
12년 전 조회 1,276
12년 전 조회 1,172
12년 전 조회 1,634
12년 전 조회 1,132
12년 전 조회 1,268
12년 전 조회 3,552
12년 전 조회 1,459
12년 전 조회 2,126
12년 전 조회 1,153
12년 전 조회 1,313
12년 전 조회 3,025
12년 전 조회 872
12년 전 조회 1,342
12년 전 조회 1,606
12년 전 조회 2,035
12년 전 조회 2,136
12년 전 조회 2,562
12년 전 조회 1,054
12년 전 조회 1,077
12년 전 조회 4,445
12년 전 조회 1,496
12년 전 조회 1,635
12년 전 조회 5,496
12년 전 조회 3,265
12년 전 조회 2,139
12년 전 조회 1,300
12년 전 조회 3,486
12년 전 조회 2,519
12년 전 조회 2,223
12년 전 조회 1,846
12년 전 조회 1,539
12년 전 조회 1,530
12년 전 조회 1,283
12년 전 조회 2,402
12년 전 조회 1,673
12년 전 조회 1,351
12년 전 조회 2,436
12년 전 조회 1,615
12년 전 조회 2,227
12년 전 조회 3,447
12년 전 조회 1,230
12년 전 조회 2,203
12년 전 조회 1,042
12년 전 조회 1,528
12년 전 조회 2,119
12년 전 조회 1,751
12년 전 조회 4,565
12년 전 조회 2,663
12년 전 조회 2,829
12년 전 조회 1,228
12년 전 조회 8,541
12년 전 조회 3,336
12년 전 조회 1,439
12년 전 조회 1,287
12년 전 조회 2,686
12년 전 조회 5,362
12년 전 조회 3,412
🐛 버그신고