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

· 12년 전 · 2638

<?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년 전 조회 4,979
12년 전 조회 9,844
12년 전 조회 5,452
12년 전 조회 5,043
12년 전 조회 2,626
12년 전 조회 2,713
12년 전 조회 4,442
12년 전 조회 5,273
12년 전 조회 5,676
12년 전 조회 2,875
12년 전 조회 2,595
12년 전 조회 2,217
12년 전 조회 2,249
12년 전 조회 4,024
12년 전 조회 4,011
12년 전 조회 2,576
12년 전 조회 4,103
12년 전 조회 2,567
12년 전 조회 9,702
12년 전 조회 2,857
12년 전 조회 5,067
12년 전 조회 7,679
12년 전 조회 2,849
12년 전 조회 4,164
12년 전 조회 1,873
12년 전 조회 2,549
12년 전 조회 2,414
12년 전 조회 2,601
12년 전 조회 4,606
12년 전 조회 2,380
12년 전 조회 3,105
12년 전 조회 2,533
12년 전 조회 2,206
12년 전 조회 2,652
12년 전 조회 3,390
12년 전 조회 2,643
12년 전 조회 2,430
12년 전 조회 2,970
12년 전 조회 1.6만
12년 전 조회 2,249
12년 전 조회 2,060
12년 전 조회 3,848
12년 전 조회 1만
12년 전 조회 2,415
12년 전 조회 2,355
12년 전 조회 2,804
12년 전 조회 2,263
12년 전 조회 2,422
12년 전 조회 4,695
12년 전 조회 2,639
12년 전 조회 3,302
12년 전 조회 2,299
12년 전 조회 2,459
12년 전 조회 4,176
12년 전 조회 2,047
12년 전 조회 2,524
12년 전 조회 2,846
12년 전 조회 3,281
13년 전 조회 3,395
13년 전 조회 3,858
13년 전 조회 2,293
13년 전 조회 2,305
13년 전 조회 5,697
13년 전 조회 2,755
13년 전 조회 2,923
13년 전 조회 6,786
13년 전 조회 4,497
13년 전 조회 3,418
13년 전 조회 2,613
13년 전 조회 4,774
13년 전 조회 3,767
13년 전 조회 3,469
13년 전 조회 3,117
13년 전 조회 2,815
13년 전 조회 2,799
13년 전 조회 2,530
13년 전 조회 3,656
13년 전 조회 2,952
13년 전 조회 2,606
13년 전 조회 3,670
13년 전 조회 2,893
13년 전 조회 3,488
13년 전 조회 4,719
13년 전 조회 2,481
13년 전 조회 3,463
13년 전 조회 2,299
13년 전 조회 2,756
13년 전 조회 3,396
13년 전 조회 3,038
13년 전 조회 5,833
13년 전 조회 3,947
13년 전 조회 4,136
13년 전 조회 2,480
13년 전 조회 9,811
13년 전 조회 4,650
13년 전 조회 2,707
13년 전 조회 2,578
13년 전 조회 3,966
13년 전 조회 6,688
13년 전 조회 4,703