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

· 12년 전 · 1490

<?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,731
12년 전 조회 8,612
12년 전 조회 4,210
12년 전 조회 3,778
12년 전 조회 1,395
12년 전 조회 1,466
12년 전 조회 3,180
12년 전 조회 4,020
12년 전 조회 4,418
12년 전 조회 1,641
12년 전 조회 1,327
12년 전 조회 911
12년 전 조회 1,014
12년 전 조회 2,751
12년 전 조회 2,765
12년 전 조회 1,284
12년 전 조회 2,827
12년 전 조회 1,310
12년 전 조회 8,490
12년 전 조회 1,538
12년 전 조회 3,801
12년 전 조회 6,417
12년 전 조회 1,599
12년 전 조회 2,863
12년 전 조회 616
12년 전 조회 1,330
12년 전 조회 1,185
12년 전 조회 1,351
12년 전 조회 3,343
12년 전 조회 1,117
12년 전 조회 1,849
12년 전 조회 1,286
12년 전 조회 933
12년 전 조회 1,421
12년 전 조회 2,131
12년 전 조회 1,417
12년 전 조회 1,297
12년 전 조회 1,819
12년 전 조회 1.5만
12년 전 조회 1,095
12년 전 조회 901
12년 전 조회 2,741
12년 전 조회 8,993
12년 전 조회 1,311
12년 전 조회 1,199
12년 전 조회 1,666
12년 전 조회 1,166
12년 전 조회 1,300
12년 전 조회 3,581
12년 전 조회 1,491
12년 전 조회 2,157
12년 전 조회 1,179
12년 전 조회 1,342
12년 전 조회 3,060
12년 전 조회 905
12년 전 조회 1,376
12년 전 조회 1,640
12년 전 조회 2,065
12년 전 조회 2,164
12년 전 조회 2,596
12년 전 조회 1,082
12년 전 조회 1,110
12년 전 조회 4,472
12년 전 조회 1,524
12년 전 조회 1,667
12년 전 조회 5,530
12년 전 조회 3,288
12년 전 조회 2,169
12년 전 조회 1,329
12년 전 조회 3,514
12년 전 조회 2,549
12년 전 조회 2,253
12년 전 조회 1,872
12년 전 조회 1,567
12년 전 조회 1,563
12년 전 조회 1,313
12년 전 조회 2,432
12년 전 조회 1,702
12년 전 조회 1,386
12년 전 조회 2,467
12년 전 조회 1,644
12년 전 조회 2,254
12년 전 조회 3,477
12년 전 조회 1,264
12년 전 조회 2,232
12년 전 조회 1,065
12년 전 조회 1,556
12년 전 조회 2,145
12년 전 조회 1,790
12년 전 조회 4,589
12년 전 조회 2,690
12년 전 조회 2,864
12년 전 조회 1,255
12년 전 조회 8,564
12년 전 조회 3,362
12년 전 조회 1,469
12년 전 조회 1,316
12년 전 조회 2,716
12년 전 조회 5,393
12년 전 조회 3,442
🐛 버그신고