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

· 12년 전 · 2467

<?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년 전 조회 4,818
12년 전 조회 9,688
12년 전 조회 5,282
12년 전 조회 4,874
12년 전 조회 2,455
12년 전 조회 2,528
12년 전 조회 4,263
12년 전 조회 5,109
12년 전 조회 5,504
12년 전 조회 2,727
12년 전 조회 2,408
12년 전 조회 2,002
12년 전 조회 2,084
12년 전 조회 3,858
12년 전 조회 3,847
12년 전 조회 2,393
12년 전 조회 3,901
12년 전 조회 2,416
12년 전 조회 9,563
12년 전 조회 2,671
12년 전 조회 4,904
12년 전 조회 7,502
12년 전 조회 2,692
12년 전 조회 3,975
12년 전 조회 1,676
12년 전 조회 2,373
12년 전 조회 2,247
12년 전 조회 2,428
12년 전 조회 4,434
12년 전 조회 2,214
12년 전 조회 2,948
12년 전 조회 2,361
12년 전 조회 2,026
12년 전 조회 2,478
12년 전 조회 3,189
12년 전 조회 2,463
12년 전 조회 2,267
12년 전 조회 2,801
12년 전 조회 1.6만
12년 전 조회 2,076
12년 전 조회 1,865
12년 전 조회 3,689
12년 전 조회 9,935
12년 전 조회 2,241
12년 전 조회 2,187
12년 전 조회 2,637
12년 전 조회 2,105
12년 전 조회 2,260
12년 전 조회 4,531
12년 전 조회 2,468
12년 전 조회 3,125
12년 전 조회 2,124
12년 전 조회 2,293
12년 전 조회 4,010
12년 전 조회 1,848
12년 전 조회 2,337
12년 전 조회 2,668
12년 전 조회 3,107
12년 전 조회 3,201
12년 전 조회 3,672
12년 전 조회 2,129
12년 전 조회 2,163
12년 전 조회 5,532
12년 전 조회 2,586
12년 전 조회 2,766
12년 전 조회 6,597
12년 전 조회 4,324
12년 전 조회 3,255
12년 전 조회 2,429
12년 전 조회 4,606
12년 전 조회 3,589
12년 전 조회 3,299
13년 전 조회 2,956
13년 전 조회 2,636
13년 전 조회 2,651
13년 전 조회 2,379
13년 전 조회 3,475
13년 전 조회 2,792
13년 전 조회 2,451
13년 전 조회 3,510
13년 전 조회 2,738
13년 전 조회 3,310
13년 전 조회 4,535
13년 전 조회 2,300
13년 전 조회 3,309
13년 전 조회 2,142
13년 전 조회 2,597
13년 전 조회 3,245
13년 전 조회 2,875
13년 전 조회 5,664
13년 전 조회 3,774
13년 전 조회 3,975
13년 전 조회 2,342
13년 전 조회 9,656
13년 전 조회 4,480
13년 전 조회 2,537
13년 전 조회 2,398
13년 전 조회 3,814
13년 전 조회 6,532
13년 전 조회 4,533