\"; echo $script; } function factory($name, $dat...","url":"https://sir.kr/boards/program/18800","author":{"@type":"Person","name":"프로프리랜서","url":"https://sir.kr/profile/c9d1f864-2b7e-4116-ac49-2d75ac933824"},"interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":7},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"datePublished":"2013-03-20T13:27:29+09:00","dateModified":"2013-03-20T13:27:29+09:00"}

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

· 12년 전 · 2765

<?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년 전 조회 5,113
12년 전 조회 9,975
12년 전 조회 5,577
12년 전 조회 5,167
12년 전 조회 2,749
12년 전 조회 2,833
12년 전 조회 4,571
12년 전 조회 5,397
12년 전 조회 5,803
12년 전 조회 3,010
12년 전 조회 2,721
12년 전 조회 2,347
12년 전 조회 2,374
12년 전 조회 4,143
12년 전 조회 4,151
12년 전 조회 2,684
12년 전 조회 4,244
12년 전 조회 2,684
12년 전 조회 9,756
12년 전 조회 2,984
12년 전 조회 5,185
12년 전 조회 7,802
12년 전 조회 2,982
12년 전 조회 4,278
12년 전 조회 1,998
12년 전 조회 2,670
12년 전 조회 2,539
12년 전 조회 2,709
12년 전 조회 4,713
12년 전 조회 2,513
12년 전 조회 3,226
12년 전 조회 2,652
12년 전 조회 2,331
12년 전 조회 2,784
12년 전 조회 3,517
12년 전 조회 2,739
12년 전 조회 2,542
12년 전 조회 3,100
12년 전 조회 1.6만
12년 전 조회 2,367
12년 전 조회 2,176
12년 전 조회 3,978
12년 전 조회 1만
12년 전 조회 2,549
12년 전 조회 2,488
12년 전 조회 2,929
12년 전 조회 2,391
12년 전 조회 2,560
12년 전 조회 4,822
12년 전 조회 2,766
12년 전 조회 3,428
12년 전 조회 2,421
12년 전 조회 2,596
13년 전 조회 4,319
13년 전 조회 2,163
13년 전 조회 2,653
13년 전 조회 2,964
13년 전 조회 3,405
13년 전 조회 3,517
13년 전 조회 3,964
13년 전 조회 2,425
13년 전 조회 2,440
13년 전 조회 5,815
13년 전 조회 2,876
13년 전 조회 3,046
13년 전 조회 6,926
13년 전 조회 4,619
13년 전 조회 3,537
13년 전 조회 2,724
13년 전 조회 4,905
13년 전 조회 3,893
13년 전 조회 3,578
13년 전 조회 3,264
13년 전 조회 2,950
13년 전 조회 2,920
13년 전 조회 2,678
13년 전 조회 3,804
13년 전 조회 3,104
13년 전 조회 2,735
13년 전 조회 3,819
13년 전 조회 3,034
13년 전 조회 3,611
13년 전 조회 4,845
13년 전 조회 2,606
13년 전 조회 3,576
13년 전 조회 2,420
13년 전 조회 2,893
13년 전 조회 3,510
13년 전 조회 3,153
13년 전 조회 5,948
13년 전 조회 4,065
13년 전 조회 4,242
13년 전 조회 2,625
13년 전 조회 9,945
13년 전 조회 4,772
13년 전 조회 2,831
13년 전 조회 2,699
13년 전 조회 4,097
13년 전 조회 6,817
13년 전 조회 4,817