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

· 12년 전 · 1624

<?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,862
12년 전 조회 8,753
12년 전 조회 4,337
12년 전 조회 3,911
12년 전 조회 1,516
12년 전 조회 1,593
12년 전 조회 3,311
12년 전 조회 4,145
12년 전 조회 4,552
12년 전 조회 1,772
12년 전 조회 1,454
12년 전 조회 1,049
12년 전 조회 1,150
12년 전 조회 2,891
12년 전 조회 2,886
12년 전 조회 1,415
12년 전 조회 2,966
12년 전 조회 1,444
12년 전 조회 8,620
12년 전 조회 1,682
12년 전 조회 3,939
12년 전 조회 6,545
12년 전 조회 1,736
12년 전 조회 2,998
12년 전 조회 755
12년 전 조회 1,459
12년 전 조회 1,320
12년 전 조회 1,475
12년 전 조회 3,473
12년 전 조회 1,255
12년 전 조회 1,984
12년 전 조회 1,419
12년 전 조회 1,080
12년 전 조회 1,556
12년 전 조회 2,267
12년 전 조회 1,547
12년 전 조회 1,441
12년 전 조회 1,967
12년 전 조회 1.5만
12년 전 조회 1,237
12년 전 조회 1,043
12년 전 조회 2,885
12년 전 조회 9,115
12년 전 조회 1,431
12년 전 조회 1,339
12년 전 조회 1,796
12년 전 조회 1,301
12년 전 조회 1,444
12년 전 조회 3,716
12년 전 조회 1,625
12년 전 조회 2,297
12년 전 조회 1,313
12년 전 조회 1,469
12년 전 조회 3,197
12년 전 조회 1,034
12년 전 조회 1,507
12년 전 조회 1,769
12년 전 조회 2,189
12년 전 조회 2,299
12년 전 조회 2,728
12년 전 조회 1,213
12년 전 조회 1,236
12년 전 조회 4,608
12년 전 조회 1,656
12년 전 조회 1,803
12년 전 조회 5,668
12년 전 조회 3,416
12년 전 조회 2,314
12년 전 조회 1,459
12년 전 조회 3,650
12년 전 조회 2,668
12년 전 조회 2,381
12년 전 조회 2,002
12년 전 조회 1,702
12년 전 조회 1,699
12년 전 조회 1,460
12년 전 조회 2,558
12년 전 조회 1,841
12년 전 조회 1,516
12년 전 조회 2,602
12년 전 조회 1,767
12년 전 조회 2,399
12년 전 조회 3,616
12년 전 조회 1,396
12년 전 조회 2,361
12년 전 조회 1,210
12년 전 조회 1,699
12년 전 조회 2,277
12년 전 조회 1,908
12년 전 조회 4,733
12년 전 조회 2,824
12년 전 조회 3,003
12년 전 조회 1,372
12년 전 조회 8,706
12년 전 조회 3,506
12년 전 조회 1,595
12년 전 조회 1,452
12년 전 조회 2,851
12년 전 조회 5,531
12년 전 조회 3,576
🐛 버그신고