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

· 12년 전 · 1460

<?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,701
12년 전 조회 8,584
12년 전 조회 4,176
12년 전 조회 3,744
12년 전 조회 1,361
12년 전 조회 1,434
12년 전 조회 3,151
12년 전 조회 3,989
12년 전 조회 4,394
12년 전 조회 1,605
12년 전 조회 1,300
12년 전 조회 883
12년 전 조회 982
12년 전 조회 2,724
12년 전 조회 2,733
12년 전 조회 1,255
12년 전 조회 2,802
12년 전 조회 1,278
12년 전 조회 8,462
12년 전 조회 1,515
12년 전 조회 3,772
12년 전 조회 6,383
12년 전 조회 1,568
12년 전 조회 2,836
12년 전 조회 589
12년 전 조회 1,298
12년 전 조회 1,159
12년 전 조회 1,316
12년 전 조회 3,309
12년 전 조회 1,086
12년 전 조회 1,820
12년 전 조회 1,256
12년 전 조회 910
12년 전 조회 1,401
12년 전 조회 2,108
12년 전 조회 1,391
12년 전 조회 1,268
12년 전 조회 1,796
12년 전 조회 1.5만
12년 전 조회 1,067
12년 전 조회 873
12년 전 조회 2,716
12년 전 조회 8,963
12년 전 조회 1,279
12년 전 조회 1,176
12년 전 조회 1,637
12년 전 조회 1,133
12년 전 조회 1,271
12년 전 조회 3,556
12년 전 조회 1,461
12년 전 조회 2,132
12년 전 조회 1,157
12년 전 조회 1,318
12년 전 조회 3,029
12년 전 조회 878
12년 전 조회 1,346
12년 전 조회 1,611
12년 전 조회 2,042
12년 전 조회 2,138
12년 전 조회 2,568
12년 전 조회 1,057
12년 전 조회 1,080
12년 전 조회 4,448
12년 전 조회 1,502
12년 전 조회 1,641
12년 전 조회 5,501
12년 전 조회 3,271
12년 전 조회 2,143
12년 전 조회 1,305
12년 전 조회 3,490
12년 전 조회 2,525
12년 전 조회 2,228
12년 전 조회 1,852
12년 전 조회 1,545
12년 전 조회 1,535
12년 전 조회 1,289
12년 전 조회 2,406
12년 전 조회 1,680
12년 전 조회 1,354
12년 전 조회 2,443
12년 전 조회 1,620
12년 전 조회 2,229
12년 전 조회 3,452
12년 전 조회 1,235
12년 전 조회 2,207
12년 전 조회 1,047
12년 전 조회 1,531
12년 전 조회 2,124
12년 전 조회 1,758
12년 전 조회 4,570
12년 전 조회 2,669
12년 전 조회 2,838
12년 전 조회 1,231
12년 전 조회 8,548
12년 전 조회 3,341
12년 전 조회 1,442
12년 전 조회 1,294
12년 전 조회 2,691
12년 전 조회 5,366
12년 전 조회 3,417
🐛 버그신고