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

· 13년 전 · 2795

<?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,142
12년 전 조회 1만
12년 전 조회 5,605
12년 전 조회 5,196
12년 전 조회 2,787
12년 전 조회 2,856
12년 전 조회 4,611
12년 전 조회 5,431
12년 전 조회 5,832
12년 전 조회 3,046
12년 전 조회 2,744
12년 전 조회 2,375
12년 전 조회 2,412
12년 전 조회 4,174
12년 전 조회 4,177
12년 전 조회 2,717
12년 전 조회 4,273
12년 전 조회 2,714
12년 전 조회 9,771
12년 전 조회 3,014
12년 전 조회 5,217
12년 전 조회 7,836
12년 전 조회 3,012
12년 전 조회 4,310
12년 전 조회 2,031
12년 전 조회 2,702
12년 전 조회 2,557
12년 전 조회 2,749
12년 전 조회 4,747
12년 전 조회 2,539
12년 전 조회 3,254
12년 전 조회 2,676
12년 전 조회 2,356
12년 전 조회 2,809
12년 전 조회 3,553
12년 전 조회 2,772
12년 전 조회 2,575
12년 전 조회 3,135
12년 전 조회 1.6만
12년 전 조회 2,398
12년 전 조회 2,196
12년 전 조회 4,001
12년 전 조회 1만
12년 전 조회 2,577
13년 전 조회 2,511
13년 전 조회 2,956
13년 전 조회 2,411
13년 전 조회 2,590
13년 전 조회 4,850
13년 전 조회 2,796
13년 전 조회 3,457
13년 전 조회 2,454
13년 전 조회 2,626
13년 전 조회 4,346
13년 전 조회 2,191
13년 전 조회 2,694
13년 전 조회 2,998
13년 전 조회 3,441
13년 전 조회 3,549
13년 전 조회 3,997
13년 전 조회 2,459
13년 전 조회 2,466
13년 전 조회 5,854
13년 전 조회 2,903
13년 전 조회 3,076
13년 전 조회 6,948
13년 전 조회 4,650
13년 전 조회 3,570
13년 전 조회 2,750
13년 전 조회 4,928
13년 전 조회 3,937
13년 전 조회 3,619
13년 전 조회 3,299
13년 전 조회 2,981
13년 전 조회 2,946
13년 전 조회 2,709
13년 전 조회 3,834
13년 전 조회 3,145
13년 전 조회 2,772
13년 전 조회 3,850
13년 전 조회 3,064
13년 전 조회 3,650
13년 전 조회 4,879
13년 전 조회 2,638
13년 전 조회 3,600
13년 전 조회 2,443
13년 전 조회 2,934
13년 전 조회 3,549
13년 전 조회 3,188
13년 전 조회 5,998
13년 전 조회 4,102
13년 전 조회 4,274
13년 전 조회 2,654
13년 전 조회 9,968
13년 전 조회 4,797
13년 전 조회 2,865
13년 전 조회 2,732
13년 전 조회 4,128
13년 전 조회 6,853
13년 전 조회 4,848