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

· 12년 전 · 1692

<?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년 전 조회 3,936
12년 전 조회 8,818
12년 전 조회 4,394
12년 전 조회 3,987
12년 전 조회 1,596
12년 전 조회 1,658
12년 전 조회 3,381
12년 전 조회 4,223
12년 전 조회 4,619
12년 전 조회 1,838
12년 전 조회 1,523
12년 전 조회 1,115
12년 전 조회 1,209
12년 전 조회 2,961
12년 전 조회 2,966
12년 전 조회 1,487
12년 전 조회 3,033
12년 전 조회 1,515
12년 전 조회 8,691
12년 전 조회 1,754
12년 전 조회 4,006
12년 전 조회 6,631
12년 전 조회 1,799
12년 전 조회 3,064
12년 전 조회 829
12년 전 조회 1,526
12년 전 조회 1,383
12년 전 조회 1,535
12년 전 조회 3,541
12년 전 조회 1,325
12년 전 조회 2,055
12년 전 조회 1,493
12년 전 조회 1,146
12년 전 조회 1,619
12년 전 조회 2,344
12년 전 조회 1,615
12년 전 조회 1,520
12년 전 조회 2,035
12년 전 조회 1.5만
12년 전 조회 1,295
12년 전 조회 1,109
12년 전 조회 2,953
12년 전 조회 9,183
12년 전 조회 1,496
12년 전 조회 1,412
12년 전 조회 1,865
12년 전 조회 1,370
12년 전 조회 1,510
12년 전 조회 3,791
12년 전 조회 1,693
12년 전 조회 2,372
12년 전 조회 1,397
12년 전 조회 1,547
12년 전 조회 3,255
12년 전 조회 1,107
12년 전 조회 1,579
12년 전 조회 1,832
12년 전 조회 2,262
12년 전 조회 2,381
12년 전 조회 2,799
12년 전 조회 1,279
12년 전 조회 1,302
12년 전 조회 4,685
12년 전 조회 1,730
12년 전 조회 1,876
12년 전 조회 5,738
12년 전 조회 3,474
12년 전 조회 2,381
12년 전 조회 1,531
12년 전 조회 3,717
12년 전 조회 2,741
12년 전 조회 2,452
12년 전 조회 2,085
12년 전 조회 1,769
12년 전 조회 1,762
12년 전 조회 1,515
12년 전 조회 2,641
12년 전 조회 1,914
12년 전 조회 1,597
12년 전 조회 2,677
12년 전 조회 1,852
12년 전 조회 2,466
12년 전 조회 3,688
12년 전 조회 1,475
12년 전 조회 2,431
12년 전 조회 1,282
12년 전 조회 1,774
12년 전 조회 2,343
12년 전 조회 1,986
12년 전 조회 4,799
12년 전 조회 2,899
12년 전 조회 3,080
12년 전 조회 1,442
12년 전 조회 8,762
12년 전 조회 3,585
12년 전 조회 1,664
12년 전 조회 1,532
12년 전 조회 2,917
12년 전 조회 5,587
12년 전 조회 3,642