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

· 13년 전 · 2875

<?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년 전 조회 5,206
12년 전 조회 1만
12년 전 조회 5,658
12년 전 조회 5,270
12년 전 조회 2,847
12년 전 조회 2,927
12년 전 조회 4,688
12년 전 조회 5,502
12년 전 조회 5,910
12년 전 조회 3,107
12년 전 조회 2,813
12년 전 조회 2,424
12년 전 조회 2,475
12년 전 조회 4,237
12년 전 조회 4,243
12년 전 조회 2,770
12년 전 조회 4,323
12년 전 조회 2,780
12년 전 조회 9,808
12년 전 조회 3,074
12년 전 조회 5,278
12년 전 조회 7,923
12년 전 조회 3,096
12년 전 조회 4,371
12년 전 조회 2,093
12년 전 조회 2,774
12년 전 조회 2,637
12년 전 조회 2,835
12년 전 조회 4,818
12년 전 조회 2,610
12년 전 조회 3,320
12년 전 조회 2,744
12년 전 조회 2,439
12년 전 조회 2,876
12년 전 조회 3,628
13년 전 조회 2,831
13년 전 조회 2,648
13년 전 조회 3,208
13년 전 조회 1.6만
13년 전 조회 2,469
13년 전 조회 2,267
13년 전 조회 4,079
13년 전 조회 1만
13년 전 조회 2,645
13년 전 조회 2,603
13년 전 조회 3,041
13년 전 조회 2,494
13년 전 조회 2,651
13년 전 조회 4,929
13년 전 조회 2,876
13년 전 조회 3,532
13년 전 조회 2,512
13년 전 조회 2,693
13년 전 조회 4,426
13년 전 조회 2,259
13년 전 조회 2,767
13년 전 조회 3,072
13년 전 조회 3,509
13년 전 조회 3,612
13년 전 조회 4,068
13년 전 조회 2,517
13년 전 조회 2,535
13년 전 조회 5,932
13년 전 조회 2,975
13년 전 조회 3,141
13년 전 조회 7,020
13년 전 조회 4,697
13년 전 조회 3,651
13년 전 조회 2,822
13년 전 조회 5,003
13년 전 조회 4,012
13년 전 조회 3,687
13년 전 조회 3,370
13년 전 조회 3,042
13년 전 조회 3,023
13년 전 조회 2,781
13년 전 조회 3,911
13년 전 조회 3,221
13년 전 조회 2,837
13년 전 조회 3,925
13년 전 조회 3,131
13년 전 조회 3,731
13년 전 조회 4,952
13년 전 조회 2,711
13년 전 조회 3,678
13년 전 조회 2,513
13년 전 조회 3,001
13년 전 조회 3,617
13년 전 조회 3,267
13년 전 조회 6,075
13년 전 조회 4,184
13년 전 조회 4,357
13년 전 조회 2,738
13년 전 조회 1만
13년 전 조회 4,872
13년 전 조회 2,950
13년 전 조회 2,820
13년 전 조회 4,216
13년 전 조회 6,938
13년 전 조회 4,930