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

· 13년 전 · 2871

<?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,201
12년 전 조회 1만
12년 전 조회 5,655
12년 전 조회 5,268
12년 전 조회 2,844
12년 전 조회 2,921
12년 전 조회 4,679
12년 전 조회 5,497
12년 전 조회 5,904
12년 전 조회 3,104
12년 전 조회 2,808
12년 전 조회 2,420
12년 전 조회 2,471
12년 전 조회 4,233
12년 전 조회 4,238
12년 전 조회 2,767
12년 전 조회 4,318
12년 전 조회 2,776
12년 전 조회 9,805
12년 전 조회 3,070
12년 전 조회 5,270
12년 전 조회 7,915
12년 전 조회 3,089
12년 전 조회 4,367
12년 전 조회 2,088
12년 전 조회 2,769
12년 전 조회 2,630
12년 전 조회 2,825
12년 전 조회 4,817
12년 전 조회 2,601
12년 전 조회 3,314
12년 전 조회 2,737
12년 전 조회 2,436
12년 전 조회 2,872
12년 전 조회 3,623
13년 전 조회 2,826
13년 전 조회 2,640
13년 전 조회 3,207
13년 전 조회 1.6만
13년 전 조회 2,462
13년 전 조회 2,260
13년 전 조회 4,074
13년 전 조회 1만
13년 전 조회 2,640
13년 전 조회 2,591
13년 전 조회 3,034
13년 전 조회 2,484
13년 전 조회 2,642
13년 전 조회 4,924
13년 전 조회 2,872
13년 전 조회 3,526
13년 전 조회 2,506
13년 전 조회 2,689
13년 전 조회 4,421
13년 전 조회 2,255
13년 전 조회 2,760
13년 전 조회 3,070
13년 전 조회 3,501
13년 전 조회 3,608
13년 전 조회 4,064
13년 전 조회 2,509
13년 전 조회 2,530
13년 전 조회 5,928
13년 전 조회 2,969
13년 전 조회 3,137
13년 전 조회 7,013
13년 전 조회 4,692
13년 전 조회 3,643
13년 전 조회 2,815
13년 전 조회 4,994
13년 전 조회 4,003
13년 전 조회 3,683
13년 전 조회 3,363
13년 전 조회 3,037
13년 전 조회 3,014
13년 전 조회 2,773
13년 전 조회 3,904
13년 전 조회 3,210
13년 전 조회 2,833
13년 전 조회 3,921
13년 전 조회 3,124
13년 전 조회 3,723
13년 전 조회 4,947
13년 전 조회 2,706
13년 전 조회 3,671
13년 전 조회 2,506
13년 전 조회 2,995
13년 전 조회 3,609
13년 전 조회 3,265
13년 전 조회 6,065
13년 전 조회 4,180
13년 전 조회 4,350
13년 전 조회 2,731
13년 전 조회 1만
13년 전 조회 4,866
13년 전 조회 2,939
13년 전 조회 2,812
13년 전 조회 4,207
13년 전 조회 6,931
13년 전 조회 4,921