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

· 12년 전 · 1502

<?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,746
12년 전 조회 8,622
12년 전 조회 4,228
12년 전 조회 3,793
12년 전 조회 1,404
12년 전 조회 1,478
12년 전 조회 3,190
12년 전 조회 4,031
12년 전 조회 4,435
12년 전 조회 1,654
12년 전 조회 1,340
12년 전 조회 924
12년 전 조회 1,026
12년 전 조회 2,761
12년 전 조회 2,777
12년 전 조회 1,304
12년 전 조회 2,840
12년 전 조회 1,323
12년 전 조회 8,504
12년 전 조회 1,552
12년 전 조회 3,815
12년 전 조회 6,429
12년 전 조회 1,612
12년 전 조회 2,874
12년 전 조회 629
12년 전 조회 1,339
12년 전 조회 1,198
12년 전 조회 1,364
12년 전 조회 3,359
12년 전 조회 1,135
12년 전 조회 1,856
12년 전 조회 1,295
12년 전 조회 944
12년 전 조회 1,431
12년 전 조회 2,142
12년 전 조회 1,425
12년 전 조회 1,312
12년 전 조회 1,837
12년 전 조회 1.5만
12년 전 조회 1,106
12년 전 조회 914
12년 전 조회 2,754
12년 전 조회 9,006
12년 전 조회 1,319
12년 전 조회 1,210
12년 전 조회 1,679
12년 전 조회 1,183
12년 전 조회 1,322
12년 전 조회 3,596
12년 전 조회 1,503
12년 전 조회 2,174
12년 전 조회 1,193
12년 전 조회 1,346
12년 전 조회 3,068
12년 전 조회 916
12년 전 조회 1,389
12년 전 조회 1,655
12년 전 조회 2,078
12년 전 조회 2,175
12년 전 조회 2,608
12년 전 조회 1,096
12년 전 조회 1,123
12년 전 조회 4,487
12년 전 조회 1,537
12년 전 조회 1,684
12년 전 조회 5,543
12년 전 조회 3,300
12년 전 조회 2,180
12년 전 조회 1,340
12년 전 조회 3,524
12년 전 조회 2,564
12년 전 조회 2,258
12년 전 조회 1,880
12년 전 조회 1,579
12년 전 조회 1,579
12년 전 조회 1,326
12년 전 조회 2,444
12년 전 조회 1,714
12년 전 조회 1,397
12년 전 조회 2,473
12년 전 조회 1,650
12년 전 조회 2,265
12년 전 조회 3,486
12년 전 조회 1,270
12년 전 조회 2,246
12년 전 조회 1,075
12년 전 조회 1,569
12년 전 조회 2,153
12년 전 조회 1,798
12년 전 조회 4,600
12년 전 조회 2,697
12년 전 조회 2,876
12년 전 조회 1,264
12년 전 조회 8,574
12년 전 조회 3,368
12년 전 조회 1,474
12년 전 조회 1,323
12년 전 조회 2,724
12년 전 조회 5,404
12년 전 조회 3,452
🐛 버그신고