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

· 12년 전 · 1619

<?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,857
12년 전 조회 8,747
12년 전 조회 4,333
12년 전 조회 3,909
12년 전 조회 1,508
12년 전 조회 1,586
12년 전 조회 3,300
12년 전 조회 4,140
12년 전 조회 4,544
12년 전 조회 1,769
12년 전 조회 1,448
12년 전 조회 1,045
12년 전 조회 1,143
12년 전 조회 2,883
12년 전 조회 2,882
12년 전 조회 1,408
12년 전 조회 2,955
12년 전 조회 1,437
12년 전 조회 8,611
12년 전 조회 1,673
12년 전 조회 3,932
12년 전 조회 6,537
12년 전 조회 1,726
12년 전 조회 2,993
12년 전 조회 750
12년 전 조회 1,452
12년 전 조회 1,310
12년 전 조회 1,467
12년 전 조회 3,467
12년 전 조회 1,247
12년 전 조회 1,976
12년 전 조회 1,414
12년 전 조회 1,071
12년 전 조회 1,551
12년 전 조회 2,264
12년 전 조회 1,542
12년 전 조회 1,435
12년 전 조회 1,955
12년 전 조회 1.5만
12년 전 조회 1,231
12년 전 조회 1,038
12년 전 조회 2,879
12년 전 조회 9,109
12년 전 조회 1,425
12년 전 조회 1,334
12년 전 조회 1,789
12년 전 조회 1,298
12년 전 조회 1,437
12년 전 조회 3,710
12년 전 조회 1,620
12년 전 조회 2,287
12년 전 조회 1,306
12년 전 조회 1,459
12년 전 조회 3,193
12년 전 조회 1,029
12년 전 조회 1,503
12년 전 조회 1,762
12년 전 조회 2,186
12년 전 조회 2,293
12년 전 조회 2,720
12년 전 조회 1,209
12년 전 조회 1,232
12년 전 조회 4,603
12년 전 조회 1,644
12년 전 조회 1,796
12년 전 조회 5,661
12년 전 조회 3,411
12년 전 조회 2,309
12년 전 조회 1,450
12년 전 조회 3,642
12년 전 조회 2,663
12년 전 조회 2,376
12년 전 조회 1,990
12년 전 조회 1,696
12년 전 조회 1,692
12년 전 조회 1,455
12년 전 조회 2,554
12년 전 조회 1,835
12년 전 조회 1,508
12년 전 조회 2,588
12년 전 조회 1,758
12년 전 조회 2,389
12년 전 조회 3,610
12년 전 조회 1,391
12년 전 조회 2,355
12년 전 조회 1,203
12년 전 조회 1,692
12년 전 조회 2,274
12년 전 조회 1,903
12년 전 조회 4,725
12년 전 조회 2,817
12년 전 조회 2,991
12년 전 조회 1,369
12년 전 조회 8,697
12년 전 조회 3,499
12년 전 조회 1,585
12년 전 조회 1,444
12년 전 조회 2,844
12년 전 조회 5,527
12년 전 조회 3,569
🐛 버그신고