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

<?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,102
12년 전 조회 9,959
12년 전 조회 5,565
12년 전 조회 5,155
12년 전 조회 2,735
12년 전 조회 2,813
12년 전 조회 4,555
12년 전 조회 5,386
12년 전 조회 5,788
12년 전 조회 2,988
12년 전 조회 2,702
12년 전 조회 2,335
12년 전 조회 2,351
12년 전 조회 4,125
12년 전 조회 4,140
12년 전 조회 2,667
12년 전 조회 4,221
12년 전 조회 2,668
12년 전 조회 9,746
12년 전 조회 2,971
12년 전 조회 5,163
12년 전 조회 7,788
12년 전 조회 2,964
12년 전 조회 4,262
12년 전 조회 1,980
12년 전 조회 2,656
12년 전 조회 2,527
12년 전 조회 2,697
12년 전 조회 4,698
12년 전 조회 2,494
12년 전 조회 3,210
12년 전 조회 2,638
12년 전 조회 2,315
12년 전 조회 2,760
12년 전 조회 3,504
12년 전 조회 2,727
12년 전 조회 2,528
12년 전 조회 3,086
12년 전 조회 1.6만
12년 전 조회 2,359
12년 전 조회 2,167
12년 전 조회 3,966
12년 전 조회 1만
12년 전 조회 2,524
12년 전 조회 2,470
12년 전 조회 2,911
12년 전 조회 2,372
12년 전 조회 2,543
12년 전 조회 4,803
12년 전 조회 2,754
12년 전 조회 3,413
12년 전 조회 2,401
12년 전 조회 2,576
12년 전 조회 4,303
13년 전 조회 2,146
13년 전 조회 2,642
13년 전 조회 2,948
13년 전 조회 3,395
13년 전 조회 3,502
13년 전 조회 3,947
13년 전 조회 2,403
13년 전 조회 2,419
13년 전 조회 5,799
13년 전 조회 2,855
13년 전 조회 3,030
13년 전 조회 6,905
13년 전 조회 4,604
13년 전 조회 3,520
13년 전 조회 2,710
13년 전 조회 4,886
13년 전 조회 3,870
13년 전 조회 3,564
13년 전 조회 3,242
13년 전 조회 2,934
13년 전 조회 2,907
13년 전 조회 2,659
13년 전 조회 3,791
13년 전 조회 3,085
13년 전 조회 2,718
13년 전 조회 3,799
13년 전 조회 3,010
13년 전 조회 3,598
13년 전 조회 4,832
13년 전 조회 2,591
13년 전 조회 3,560
13년 전 조회 2,403
13년 전 조회 2,871
13년 전 조회 3,496
13년 전 조회 3,139
13년 전 조회 5,939
13년 전 조회 4,047
13년 전 조회 4,227
13년 전 조회 2,602
13년 전 조회 9,930
13년 전 조회 4,755
13년 전 조회 2,818
13년 전 조회 2,684
13년 전 조회 4,082
13년 전 조회 6,800
13년 전 조회 4,806