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

· 12년 전 · 1476

<?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,713
12년 전 조회 8,592
12년 전 조회 4,196
12년 전 조회 3,760
12년 전 조회 1,376
12년 전 조회 1,449
12년 전 조회 3,167
12년 전 조회 4,002
12년 전 조회 4,410
12년 전 조회 1,622
12년 전 조회 1,310
12년 전 조회 897
12년 전 조회 999
12년 전 조회 2,740
12년 전 조회 2,749
12년 전 조회 1,273
12년 전 조회 2,818
12년 전 조회 1,293
12년 전 조회 8,476
12년 전 조회 1,528
12년 전 조회 3,785
12년 전 조회 6,400
12년 전 조회 1,582
12년 전 조회 2,852
12년 전 조회 600
12년 전 조회 1,312
12년 전 조회 1,172
12년 전 조회 1,332
12년 전 조회 3,324
12년 전 조회 1,100
12년 전 조회 1,833
12년 전 조회 1,273
12년 전 조회 924
12년 전 조회 1,408
12년 전 조회 2,120
12년 전 조회 1,403
12년 전 조회 1,285
12년 전 조회 1,808
12년 전 조회 1.5만
12년 전 조회 1,084
12년 전 조회 886
12년 전 조회 2,729
12년 전 조회 8,974
12년 전 조회 1,297
12년 전 조회 1,187
12년 전 조회 1,650
12년 전 조회 1,154
12년 전 조회 1,286
12년 전 조회 3,568
12년 전 조회 1,477
12년 전 조회 2,144
12년 전 조회 1,169
12년 전 조회 1,332
12년 전 조회 3,046
12년 전 조회 890
12년 전 조회 1,358
12년 전 조회 1,623
12년 전 조회 2,052
12년 전 조회 2,149
12년 전 조회 2,581
12년 전 조회 1,071
12년 전 조회 1,095
12년 전 조회 4,462
12년 전 조회 1,512
12년 전 조회 1,652
12년 전 조회 5,513
12년 전 조회 3,277
12년 전 조회 2,159
12년 전 조회 1,316
12년 전 조회 3,505
12년 전 조회 2,536
12년 전 조회 2,242
12년 전 조회 1,860
12년 전 조회 1,557
12년 전 조회 1,549
12년 전 조회 1,303
12년 전 조회 2,420
12년 전 조회 1,695
12년 전 조회 1,370
12년 전 조회 2,453
12년 전 조회 1,633
12년 전 조회 2,244
12년 전 조회 3,461
12년 전 조회 1,253
12년 전 조회 2,221
12년 전 조회 1,056
12년 전 조회 1,546
12년 전 조회 2,136
12년 전 조회 1,772
12년 전 조회 4,580
12년 전 조회 2,680
12년 전 조회 2,849
12년 전 조회 1,246
12년 전 조회 8,557
12년 전 조회 3,354
12년 전 조회 1,453
12년 전 조회 1,305
12년 전 조회 2,704
12년 전 조회 5,382
12년 전 조회 3,433
🐛 버그신고