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

· 12년 전 · 1451

<?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,687
12년 전 조회 8,571
12년 전 조회 4,169
12년 전 조회 3,733
12년 전 조회 1,349
12년 전 조회 1,418
12년 전 조회 3,141
12년 전 조회 3,977
12년 전 조회 4,383
12년 전 조회 1,597
12년 전 조회 1,288
12년 전 조회 872
12년 전 조회 972
12년 전 조회 2,710
12년 전 조회 2,724
12년 전 조회 1,246
12년 전 조회 2,791
12년 전 조회 1,269
12년 전 조회 8,452
12년 전 조회 1,506
12년 전 조회 3,764
12년 전 조회 6,375
12년 전 조회 1,558
12년 전 조회 2,827
12년 전 조회 578
12년 전 조회 1,284
12년 전 조회 1,147
12년 전 조회 1,305
12년 전 조회 3,298
12년 전 조회 1,074
12년 전 조회 1,809
12년 전 조회 1,246
12년 전 조회 898
12년 전 조회 1,394
12년 전 조회 2,096
12년 전 조회 1,376
12년 전 조회 1,259
12년 전 조회 1,783
12년 전 조회 1.5만
12년 전 조회 1,056
12년 전 조회 857
12년 전 조회 2,702
12년 전 조회 8,951
12년 전 조회 1,273
12년 전 조회 1,165
12년 전 조회 1,623
12년 전 조회 1,128
12년 전 조회 1,258
12년 전 조회 3,544
12년 전 조회 1,452
12년 전 조회 2,118
12년 전 조회 1,144
12년 전 조회 1,310
12년 전 조회 3,020
12년 전 조회 864
12년 전 조회 1,332
12년 전 조회 1,599
12년 전 조회 2,025
12년 전 조회 2,128
12년 전 조회 2,556
12년 전 조회 1,052
12년 전 조회 1,072
12년 전 조회 4,434
12년 전 조회 1,488
12년 전 조회 1,627
12년 전 조회 5,487
12년 전 조회 3,263
12년 전 조회 2,133
12년 전 조회 1,293
12년 전 조회 3,481
12년 전 조회 2,511
12년 전 조회 2,221
12년 전 조회 1,836
12년 전 조회 1,532
12년 전 조회 1,524
12년 전 조회 1,278
12년 전 조회 2,394
12년 전 조회 1,666
12년 전 조회 1,343
12년 전 조회 2,429
12년 전 조회 1,605
12년 전 조회 2,217
12년 전 조회 3,438
12년 전 조회 1,222
12년 전 조회 2,199
12년 전 조회 1,034
12년 전 조회 1,519
12년 전 조회 2,110
12년 전 조회 1,741
12년 전 조회 4,556
12년 전 조회 2,654
12년 전 조회 2,821
12년 전 조회 1,224
12년 전 조회 8,536
12년 전 조회 3,330
12년 전 조회 1,430
12년 전 조회 1,280
12년 전 조회 2,678
12년 전 조회 5,357
12년 전 조회 3,408
🐛 버그신고