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

· 12년 전 · 1501

<?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,745
12년 전 조회 8,622
12년 전 조회 4,227
12년 전 조회 3,792
12년 전 조회 1,402
12년 전 조회 1,477
12년 전 조회 3,188
12년 전 조회 4,031
12년 전 조회 4,431
12년 전 조회 1,653
12년 전 조회 1,339
12년 전 조회 920
12년 전 조회 1,025
12년 전 조회 2,759
12년 전 조회 2,775
12년 전 조회 1,300
12년 전 조회 2,836
12년 전 조회 1,322
12년 전 조회 8,501
12년 전 조회 1,549
12년 전 조회 3,813
12년 전 조회 6,428
12년 전 조회 1,611
12년 전 조회 2,872
12년 전 조회 628
12년 전 조회 1,338
12년 전 조회 1,195
12년 전 조회 1,363
12년 전 조회 3,353
12년 전 조회 1,133
12년 전 조회 1,856
12년 전 조회 1,294
12년 전 조회 943
12년 전 조회 1,430
12년 전 조회 2,141
12년 전 조회 1,423
12년 전 조회 1,311
12년 전 조회 1,835
12년 전 조회 1.5만
12년 전 조회 1,105
12년 전 조회 911
12년 전 조회 2,751
12년 전 조회 9,004
12년 전 조회 1,318
12년 전 조회 1,209
12년 전 조회 1,676
12년 전 조회 1,180
12년 전 조회 1,319
12년 전 조회 3,595
12년 전 조회 1,502
12년 전 조회 2,171
12년 전 조회 1,192
12년 전 조회 1,346
12년 전 조회 3,067
12년 전 조회 915
12년 전 조회 1,388
12년 전 조회 1,653
12년 전 조회 2,076
12년 전 조회 2,174
12년 전 조회 2,606
12년 전 조회 1,096
12년 전 조회 1,121
12년 전 조회 4,487
12년 전 조회 1,536
12년 전 조회 1,683
12년 전 조회 5,541
12년 전 조회 3,299
12년 전 조회 2,179
12년 전 조회 1,337
12년 전 조회 3,523
12년 전 조회 2,562
12년 전 조회 2,257
12년 전 조회 1,880
12년 전 조회 1,576
12년 전 조회 1,579
12년 전 조회 1,323
12년 전 조회 2,443
12년 전 조회 1,713
12년 전 조회 1,396
12년 전 조회 2,471
12년 전 조회 1,648
12년 전 조회 2,263
12년 전 조회 3,485
12년 전 조회 1,269
12년 전 조회 2,244
12년 전 조회 1,073
12년 전 조회 1,568
12년 전 조회 2,150
12년 전 조회 1,798
12년 전 조회 4,600
12년 전 조회 2,695
12년 전 조회 2,876
12년 전 조회 1,261
12년 전 조회 8,574
12년 전 조회 3,366
12년 전 조회 1,474
12년 전 조회 1,323
12년 전 조회 2,723
12년 전 조회 5,400
12년 전 조회 3,450
🐛 버그신고