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

· 12년 전 · 2728

<?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,060
12년 전 조회 9,927
12년 전 조회 5,531
12년 전 조회 5,121
12년 전 조회 2,698
12년 전 조회 2,783
12년 전 조회 4,521
12년 전 조회 5,350
12년 전 조회 5,757
12년 전 조회 2,959
12년 전 조회 2,669
12년 전 조회 2,301
12년 전 조회 2,328
12년 전 조회 4,096
12년 전 조회 4,101
12년 전 조회 2,644
12년 전 조회 4,192
12년 전 조회 2,642
12년 전 조회 9,727
12년 전 조회 2,942
12년 전 조회 5,132
12년 전 조회 7,755
12년 전 조회 2,943
12년 전 조회 4,239
12년 전 조회 1,955
12년 전 조회 2,627
12년 전 조회 2,496
12년 전 조회 2,668
12년 전 조회 4,663
12년 전 조회 2,464
12년 전 조회 3,178
12년 전 조회 2,612
12년 전 조회 2,287
12년 전 조회 2,734
12년 전 조회 3,472
12년 전 조회 2,700
12년 전 조회 2,504
12년 전 조회 3,056
12년 전 조회 1.6만
12년 전 조회 2,329
12년 전 조회 2,137
12년 전 조회 3,934
12년 전 조회 1만
12년 전 조회 2,496
12년 전 조회 2,440
12년 전 조회 2,887
12년 전 조회 2,339
12년 전 조회 2,509
12년 전 조회 4,773
12년 전 조회 2,729
12년 전 조회 3,379
12년 전 조회 2,374
12년 전 조회 2,541
12년 전 조회 4,262
12년 전 조회 2,123
12년 전 조회 2,607
13년 전 조회 2,915
13년 전 조회 3,368
13년 전 조회 3,470
13년 전 조회 3,910
13년 전 조회 2,373
13년 전 조회 2,389
13년 전 조회 5,771
13년 전 조회 2,824
13년 전 조회 2,998
13년 전 조회 6,867
13년 전 조회 4,570
13년 전 조회 3,480
13년 전 조회 2,683
13년 전 조회 4,856
13년 전 조회 3,845
13년 전 조회 3,531
13년 전 조회 3,208
13년 전 조회 2,893
13년 전 조회 2,873
13년 전 조회 2,620
13년 전 조회 3,760
13년 전 조회 3,048
13년 전 조회 2,682
13년 전 조회 3,769
13년 전 조회 2,985
13년 전 조회 3,561
13년 전 조회 4,795
13년 전 조회 2,563
13년 전 조회 3,533
13년 전 조회 2,378
13년 전 조회 2,842
13년 전 조회 3,465
13년 전 조회 3,110
13년 전 조회 5,913
13년 전 조회 4,018
13년 전 조회 4,201
13년 전 조회 2,567
13년 전 조회 9,899
13년 전 조회 4,720
13년 전 조회 2,792
13년 전 조회 2,659
13년 전 조회 4,050
13년 전 조회 6,770
13년 전 조회 4,778