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

· 12년 전 · 1462

<?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,702
12년 전 조회 8,586
12년 전 조회 4,178
12년 전 조회 3,747
12년 전 조회 1,363
12년 전 조회 1,435
12년 전 조회 3,152
12년 전 조회 3,990
12년 전 조회 4,396
12년 전 조회 1,608
12년 전 조회 1,301
12년 전 조회 884
12년 전 조회 984
12년 전 조회 2,725
12년 전 조회 2,735
12년 전 조회 1,256
12년 전 조회 2,803
12년 전 조회 1,280
12년 전 조회 8,463
12년 전 조회 1,517
12년 전 조회 3,775
12년 전 조회 6,384
12년 전 조회 1,571
12년 전 조회 2,837
12년 전 조회 591
12년 전 조회 1,299
12년 전 조회 1,160
12년 전 조회 1,318
12년 전 조회 3,310
12년 전 조회 1,086
12년 전 조회 1,821
12년 전 조회 1,259
12년 전 조회 912
12년 전 조회 1,402
12년 전 조회 2,110
12년 전 조회 1,392
12년 전 조회 1,269
12년 전 조회 1,796
12년 전 조회 1.5만
12년 전 조회 1,069
12년 전 조회 873
12년 전 조회 2,718
12년 전 조회 8,963
12년 전 조회 1,281
12년 전 조회 1,178
12년 전 조회 1,637
12년 전 조회 1,136
12년 전 조회 1,273
12년 전 조회 3,557
12년 전 조회 1,463
12년 전 조회 2,133
12년 전 조회 1,157
12년 전 조회 1,319
12년 전 조회 3,030
12년 전 조회 879
12년 전 조회 1,347
12년 전 조회 1,612
12년 전 조회 2,042
12년 전 조회 2,138
12년 전 조회 2,568
12년 전 조회 1,057
12년 전 조회 1,081
12년 전 조회 4,448
12년 전 조회 1,502
12년 전 조회 1,642
12년 전 조회 5,501
12년 전 조회 3,271
12년 전 조회 2,144
12년 전 조회 1,306
12년 전 조회 3,492
12년 전 조회 2,527
12년 전 조회 2,229
12년 전 조회 1,852
12년 전 조회 1,547
12년 전 조회 1,536
12년 전 조회 1,291
12년 전 조회 2,406
12년 전 조회 1,682
12년 전 조회 1,355
12년 전 조회 2,444
12년 전 조회 1,621
12년 전 조회 2,230
12년 전 조회 3,452
12년 전 조회 1,237
12년 전 조회 2,207
12년 전 조회 1,048
12년 전 조회 1,532
12년 전 조회 2,125
12년 전 조회 1,760
12년 전 조회 4,571
12년 전 조회 2,670
12년 전 조회 2,838
12년 전 조회 1,231
12년 전 조회 8,548
12년 전 조회 3,341
12년 전 조회 1,442
12년 전 조회 1,294
12년 전 조회 2,692
12년 전 조회 5,367
12년 전 조회 3,418
🐛 버그신고