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

· 13년 전 · 2868

<?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,198
12년 전 조회 1만
12년 전 조회 5,653
12년 전 조회 5,268
12년 전 조회 2,842
12년 전 조회 2,920
12년 전 조회 4,678
12년 전 조회 5,495
12년 전 조회 5,900
12년 전 조회 3,101
12년 전 조회 2,807
12년 전 조회 2,419
12년 전 조회 2,468
12년 전 조회 4,231
12년 전 조회 4,235
12년 전 조회 2,765
12년 전 조회 4,317
12년 전 조회 2,773
12년 전 조회 9,803
12년 전 조회 3,068
12년 전 조회 5,270
12년 전 조회 7,914
12년 전 조회 3,086
12년 전 조회 4,366
12년 전 조회 2,086
12년 전 조회 2,769
12년 전 조회 2,626
12년 전 조회 2,823
12년 전 조회 4,814
12년 전 조회 2,599
12년 전 조회 3,312
12년 전 조회 2,733
12년 전 조회 2,436
12년 전 조회 2,870
12년 전 조회 3,621
13년 전 조회 2,826
13년 전 조회 2,639
13년 전 조회 3,204
13년 전 조회 1.6만
13년 전 조회 2,459
13년 전 조회 2,259
13년 전 조회 4,072
13년 전 조회 1만
13년 전 조회 2,638
13년 전 조회 2,589
13년 전 조회 3,032
13년 전 조회 2,481
13년 전 조회 2,640
13년 전 조회 4,921
13년 전 조회 2,869
13년 전 조회 3,525
13년 전 조회 2,505
13년 전 조회 2,686
13년 전 조회 4,420
13년 전 조회 2,254
13년 전 조회 2,758
13년 전 조회 3,067
13년 전 조회 3,501
13년 전 조회 3,604
13년 전 조회 4,063
13년 전 조회 2,508
13년 전 조회 2,527
13년 전 조회 5,925
13년 전 조회 2,968
13년 전 조회 3,134
13년 전 조회 7,010
13년 전 조회 4,692
13년 전 조회 3,641
13년 전 조회 2,812
13년 전 조회 4,992
13년 전 조회 4,002
13년 전 조회 3,681
13년 전 조회 3,358
13년 전 조회 3,035
13년 전 조회 3,012
13년 전 조회 2,772
13년 전 조회 3,902
13년 전 조회 3,209
13년 전 조회 2,828
13년 전 조회 3,919
13년 전 조회 3,123
13년 전 조회 3,722
13년 전 조회 4,943
13년 전 조회 2,706
13년 전 조회 3,668
13년 전 조회 2,503
13년 전 조회 2,993
13년 전 조회 3,606
13년 전 조회 3,264
13년 전 조회 6,064
13년 전 조회 4,179
13년 전 조회 4,349
13년 전 조회 2,728
13년 전 조회 1만
13년 전 조회 4,864
13년 전 조회 2,935
13년 전 조회 2,811
13년 전 조회 4,204
13년 전 조회 6,929
13년 전 조회 4,920