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

<?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,135
12년 전 조회 9,998
12년 전 조회 5,598
12년 전 조회 5,185
12년 전 조회 2,777
12년 전 조회 2,848
12년 전 조회 4,602
12년 전 조회 5,426
12년 전 조회 5,828
12년 전 조회 3,035
12년 전 조회 2,740
12년 전 조회 2,371
12년 전 조회 2,397
12년 전 조회 4,166
12년 전 조회 4,174
12년 전 조회 2,708
12년 전 조회 4,261
12년 전 조회 2,706
12년 전 조회 9,767
12년 전 조회 3,007
12년 전 조회 5,203
12년 전 조회 7,830
12년 전 조회 3,006
12년 전 조회 4,301
12년 전 조회 2,019
12년 전 조회 2,695
12년 전 조회 2,550
12년 전 조회 2,737
12년 전 조회 4,738
12년 전 조회 2,531
12년 전 조회 3,248
12년 전 조회 2,668
12년 전 조회 2,349
12년 전 조회 2,801
12년 전 조회 3,542
12년 전 조회 2,762
12년 전 조회 2,565
12년 전 조회 3,125
12년 전 조회 1.6만
12년 전 조회 2,391
12년 전 조회 2,189
12년 전 조회 3,999
12년 전 조회 1만
12년 전 조회 2,568
12년 전 조회 2,506
12년 전 조회 2,947
12년 전 조회 2,407
12년 전 조회 2,585
12년 전 조회 4,843
12년 전 조회 2,786
12년 전 조회 3,448
13년 전 조회 2,446
13년 전 조회 2,619
13년 전 조회 4,340
13년 전 조회 2,182
13년 전 조회 2,683
13년 전 조회 2,984
13년 전 조회 3,432
13년 전 조회 3,542
13년 전 조회 3,992
13년 전 조회 2,454
13년 전 조회 2,461
13년 전 조회 5,848
13년 전 조회 2,901
13년 전 조회 3,070
13년 전 조회 6,943
13년 전 조회 4,644
13년 전 조회 3,568
13년 전 조회 2,742
13년 전 조회 4,926
13년 전 조회 3,927
13년 전 조회 3,611
13년 전 조회 3,289
13년 전 조회 2,975
13년 전 조회 2,943
13년 전 조회 2,698
13년 전 조회 3,829
13년 전 조회 3,137
13년 전 조회 2,765
13년 전 조회 3,841
13년 전 조회 3,053
13년 전 조회 3,639
13년 전 조회 4,872
13년 전 조회 2,630
13년 전 조회 3,595
13년 전 조회 2,439
13년 전 조회 2,925
13년 전 조회 3,543
13년 전 조회 3,178
13년 전 조회 5,987
13년 전 조회 4,096
13년 전 조회 4,269
13년 전 조회 2,650
13년 전 조회 9,961
13년 전 조회 4,791
13년 전 조회 2,858
13년 전 조회 2,721
13년 전 조회 4,118
13년 전 조회 6,843
13년 전 조회 4,835