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

· 12년 전 · 2052

<?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,344
12년 전 조회 9,240
12년 전 조회 4,797
12년 전 조회 4,409
12년 전 조회 2,017
12년 전 조회 2,056
12년 전 조회 3,800
12년 전 조회 4,642
12년 전 조회 5,044
12년 전 조회 2,272
12년 전 조회 1,939
12년 전 조회 1,520
12년 전 조회 1,594
12년 전 조회 3,386
12년 전 조회 3,376
12년 전 조회 1,898
12년 전 조회 3,452
12년 전 조회 1,907
12년 전 조회 9,201
12년 전 조회 2,176
12년 전 조회 4,390
12년 전 조회 7,045
12년 전 조회 2,203
12년 전 조회 3,495
12년 전 조회 1,216
12년 전 조회 1,920
12년 전 조회 1,795
12년 전 조회 1,962
12년 전 조회 3,954
12년 전 조회 1,723
12년 전 조회 2,467
12년 전 조회 1,919
12년 전 조회 1,547
12년 전 조회 1,979
12년 전 조회 2,716
12년 전 조회 2,017
12년 전 조회 1,877
12년 전 조회 2,386
12년 전 조회 1.5만
12년 전 조회 1,648
12년 전 조회 1,418
12년 전 조회 3,286
12년 전 조회 9,512
12년 전 조회 1,830
12년 전 조회 1,779
12년 전 조회 2,223
12년 전 조회 1,701
12년 전 조회 1,873
12년 전 조회 4,157
12년 전 조회 2,053
12년 전 조회 2,717
12년 전 조회 1,722
12년 전 조회 1,886
12년 전 조회 3,594
12년 전 조회 1,436
12년 전 조회 1,913
12년 전 조회 2,216
12년 전 조회 2,647
12년 전 조회 2,749
12년 전 조회 3,189
12년 전 조회 1,644
12년 전 조회 1,700
12년 전 조회 5,060
12년 전 조회 2,124
12년 전 조회 2,299
12년 전 조회 6,125
12년 전 조회 3,835
12년 전 조회 2,786
12년 전 조회 1,933
12년 전 조회 4,113
12년 전 조회 3,146
12년 전 조회 2,803
12년 전 조회 2,493
12년 전 조회 2,175
12년 전 조회 2,180
12년 전 조회 1,918
12년 전 조회 3,008
12년 전 조회 2,316
12년 전 조회 2,020
12년 전 조회 3,085
12년 전 조회 2,279
12년 전 조회 2,859
12년 전 조회 4,078
12년 전 조회 1,847
12년 전 조회 2,816
12년 전 조회 1,666
12년 전 조회 2,172
12년 전 조회 2,783
12년 전 조회 2,398
12년 전 조회 5,200
12년 전 조회 3,316
12년 전 조회 3,506
12년 전 조회 1,854
12년 전 조회 9,177
12년 전 조회 3,998
12년 전 조회 2,048
12년 전 조회 1,926
12년 전 조회 3,351
12년 전 조회 6,027
12년 전 조회 4,045