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

· 12년 전 · 1553

<?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,798
12년 전 조회 8,682
12년 전 조회 4,275
12년 전 조회 3,847
12년 전 조회 1,453
12년 전 조회 1,537
12년 전 조회 3,241
12년 전 조회 4,078
12년 전 조회 4,482
12년 전 조회 1,705
12년 전 조회 1,392
12년 전 조회 985
12년 전 조회 1,088
12년 전 조회 2,819
12년 전 조회 2,833
12년 전 조회 1,349
12년 전 조회 2,898
12년 전 조회 1,377
12년 전 조회 8,549
12년 전 조회 1,607
12년 전 조회 3,874
12년 전 조회 6,471
12년 전 조회 1,666
12년 전 조회 2,923
12년 전 조회 697
12년 전 조회 1,399
12년 전 조회 1,247
12년 전 조회 1,410
12년 전 조회 3,408
12년 전 조회 1,182
12년 전 조회 1,912
12년 전 조회 1,349
12년 전 조회 1,000
12년 전 조회 1,483
12년 전 조회 2,207
12년 전 조회 1,486
12년 전 조회 1,367
12년 전 조회 1,893
12년 전 조회 1.5만
12년 전 조회 1,161
12년 전 조회 978
12년 전 조회 2,807
12년 전 조회 9,053
12년 전 조회 1,366
12년 전 조회 1,271
12년 전 조회 1,732
12년 전 조회 1,244
12년 전 조회 1,378
12년 전 조회 3,649
12년 전 조회 1,554
12년 전 조회 2,227
12년 전 조회 1,244
12년 전 조회 1,394
12년 전 조회 3,126
12년 전 조회 975
12년 전 조회 1,440
12년 전 조회 1,700
12년 전 조회 2,124
12년 전 조회 2,219
12년 전 조회 2,657
12년 전 조회 1,153
12년 전 조회 1,175
12년 전 조회 4,553
12년 전 조회 1,584
12년 전 조회 1,729
12년 전 조회 5,606
12년 전 조회 3,349
12년 전 조회 2,235
12년 전 조회 1,390
12년 전 조회 3,586
12년 전 조회 2,605
12년 전 조회 2,312
12년 전 조회 1,930
12년 전 조회 1,634
12년 전 조회 1,620
12년 전 조회 1,377
12년 전 조회 2,493
12년 전 조회 1,766
12년 전 조회 1,443
12년 전 조회 2,520
12년 전 조회 1,710
12년 전 조회 2,322
12년 전 조회 3,547
12년 전 조회 1,330
12년 전 조회 2,289
12년 전 조회 1,138
12년 전 조회 1,624
12년 전 조회 2,205
12년 전 조회 1,847
12년 전 조회 4,658
12년 전 조회 2,761
12년 전 조회 2,928
12년 전 조회 1,315
12년 전 조회 8,626
12년 전 조회 3,428
12년 전 조회 1,521
12년 전 조회 1,379
12년 전 조회 2,777
12년 전 조회 5,459
12년 전 조회 3,510
🐛 버그신고