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

· 12년 전 · 1574

<?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,823
12년 전 조회 8,708
12년 전 조회 4,297
12년 전 조회 3,859
12년 전 조회 1,475
12년 전 조회 1,551
12년 전 조회 3,262
12년 전 조회 4,099
12년 전 조회 4,511
12년 전 조회 1,724
12년 전 조회 1,406
12년 전 조회 1,002
12년 전 조회 1,106
12년 전 조회 2,835
12년 전 조회 2,850
12년 전 조회 1,369
12년 전 조회 2,921
12년 전 조회 1,399
12년 전 조회 8,570
12년 전 조회 1,626
12년 전 조회 3,891
12년 전 조회 6,494
12년 전 조회 1,687
12년 전 조회 2,947
12년 전 조회 713
12년 전 조회 1,420
12년 전 조회 1,269
12년 전 조회 1,423
12년 전 조회 3,427
12년 전 조회 1,203
12년 전 조회 1,933
12년 전 조회 1,370
12년 전 조회 1,025
12년 전 조회 1,509
12년 전 조회 2,224
12년 전 조회 1,508
12년 전 조회 1,386
12년 전 조회 1,914
12년 전 조회 1.5만
12년 전 조회 1,184
12년 전 조회 992
12년 전 조회 2,828
12년 전 조회 9,072
12년 전 조회 1,387
12년 전 조회 1,297
12년 전 조회 1,752
12년 전 조회 1,263
12년 전 조회 1,397
12년 전 조회 3,667
12년 전 조회 1,575
12년 전 조회 2,247
12년 전 조회 1,262
12년 전 조회 1,419
12년 전 조회 3,151
12년 전 조회 997
12년 전 조회 1,462
12년 전 조회 1,724
12년 전 조회 2,143
12년 전 조회 2,238
12년 전 조회 2,680
12년 전 조회 1,171
12년 전 조회 1,198
12년 전 조회 4,561
12년 전 조회 1,604
12년 전 조회 1,758
12년 전 조회 5,631
12년 전 조회 3,374
12년 전 조회 2,264
12년 전 조회 1,413
12년 전 조회 3,600
12년 전 조회 2,623
12년 전 조회 2,332
12년 전 조회 1,953
12년 전 조회 1,651
12년 전 조회 1,642
12년 전 조회 1,399
12년 전 조회 2,506
12년 전 조회 1,790
12년 전 조회 1,464
12년 전 조회 2,537
12년 전 조회 1,723
12년 전 조회 2,342
12년 전 조회 3,568
12년 전 조회 1,350
12년 전 조회 2,307
12년 전 조회 1,159
12년 전 조회 1,642
12년 전 조회 2,229
12년 전 조회 1,864
12년 전 조회 4,678
12년 전 조회 2,787
12년 전 조회 2,950
12년 전 조회 1,332
12년 전 조회 8,650
12년 전 조회 3,450
12년 전 조회 1,545
12년 전 조회 1,397
12년 전 조회 2,798
12년 전 조회 5,480
12년 전 조회 3,527
🐛 버그신고