\"; 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년 전 · 2750

<?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,101
12년 전 조회 9,957
12년 전 조회 5,560
12년 전 조회 5,152
12년 전 조회 2,730
12년 전 조회 2,811
12년 전 조회 4,551
12년 전 조회 5,381
12년 전 조회 5,788
12년 전 조회 2,980
12년 전 조회 2,701
12년 전 조회 2,331
12년 전 조회 2,349
12년 전 조회 4,124
12년 전 조회 4,139
12년 전 조회 2,666
12년 전 조회 4,221
12년 전 조회 2,667
12년 전 조회 9,743
12년 전 조회 2,970
12년 전 조회 5,161
12년 전 조회 7,787
12년 전 조회 2,962
12년 전 조회 4,258
12년 전 조회 1,979
12년 전 조회 2,654
12년 전 조회 2,522
12년 전 조회 2,695
12년 전 조회 4,694
12년 전 조회 2,491
12년 전 조회 3,207
12년 전 조회 2,636
12년 전 조회 2,313
12년 전 조회 2,760
12년 전 조회 3,499
12년 전 조회 2,726
12년 전 조회 2,526
12년 전 조회 3,083
12년 전 조회 1.6만
12년 전 조회 2,357
12년 전 조회 2,164
12년 전 조회 3,962
12년 전 조회 1만
12년 전 조회 2,522
12년 전 조회 2,467
12년 전 조회 2,907
12년 전 조회 2,370
12년 전 조회 2,541
12년 전 조회 4,800
12년 전 조회 2,751
12년 전 조회 3,410
12년 전 조회 2,398
12년 전 조회 2,576
12년 전 조회 4,302
13년 전 조회 2,143
13년 전 조회 2,638
13년 전 조회 2,945
13년 전 조회 3,393
13년 전 조회 3,499
13년 전 조회 3,938
13년 전 조회 2,400
13년 전 조회 2,416
13년 전 조회 5,794
13년 전 조회 2,847
13년 전 조회 3,030
13년 전 조회 6,900
13년 전 조회 4,604
13년 전 조회 3,517
13년 전 조회 2,707
13년 전 조회 4,884
13년 전 조회 3,868
13년 전 조회 3,563
13년 전 조회 3,241
13년 전 조회 2,932
13년 전 조회 2,903
13년 전 조회 2,656
13년 전 조회 3,789
13년 전 조회 3,082
13년 전 조회 2,718
13년 전 조회 3,793
13년 전 조회 3,007
13년 전 조회 3,594
13년 전 조회 4,826
13년 전 조회 2,588
13년 전 조회 3,558
13년 전 조회 2,402
13년 전 조회 2,869
13년 전 조회 3,494
13년 전 조회 3,138
13년 전 조회 5,933
13년 전 조회 4,039
13년 전 조회 4,224
13년 전 조회 2,600
13년 전 조회 9,927
13년 전 조회 4,753
13년 전 조회 2,818
13년 전 조회 2,681
13년 전 조회 4,080
13년 전 조회 6,796
13년 전 조회 4,805