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

· 12년 전 · 1799

<?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,028
12년 전 조회 8,900
12년 전 조회 4,489
12년 전 조회 4,089
12년 전 조회 1,681
12년 전 조회 1,740
12년 전 조회 3,487
12년 전 조회 4,316
12년 전 조회 4,709
12년 전 조회 1,948
12년 전 조회 1,616
12년 전 조회 1,208
12년 전 조회 1,281
12년 전 조회 3,060
12년 전 조회 3,071
12년 전 조회 1,578
12년 전 조회 3,125
12년 전 조회 1,592
12년 전 조회 8,797
12년 전 조회 1,845
12년 전 조회 4,092
12년 전 조회 6,726
12년 전 조회 1,889
12년 전 조회 3,172
12년 전 조회 919
12년 전 조회 1,598
12년 전 조회 1,463
12년 전 조회 1,648
12년 전 조회 3,630
12년 전 조회 1,420
12년 전 조회 2,136
12년 전 조회 1,575
12년 전 조회 1,234
12년 전 조회 1,697
12년 전 조회 2,431
12년 전 조회 1,714
12년 전 조회 1,614
12년 전 조회 2,119
12년 전 조회 1.5만
12년 전 조회 1,384
12년 전 조회 1,188
12년 전 조회 3,026
12년 전 조회 9,272
12년 전 조회 1,565
12년 전 조회 1,522
12년 전 조회 1,961
12년 전 조회 1,461
12년 전 조회 1,610
12년 전 조회 3,903
12년 전 조회 1,800
12년 전 조회 2,464
12년 전 조회 1,481
12년 전 조회 1,629
12년 전 조회 3,349
12년 전 조회 1,189
12년 전 조회 1,649
12년 전 조회 1,915
12년 전 조회 2,340
12년 전 조회 2,459
12년 전 조회 2,892
12년 전 조회 1,352
12년 전 조회 1,385
12년 전 조회 4,766
12년 전 조회 1,808
12년 전 조회 1,964
12년 전 조회 5,811
12년 전 조회 3,547
12년 전 조회 2,455
12년 전 조회 1,623
12년 전 조회 3,791
12년 전 조회 2,827
12년 전 조회 2,521
12년 전 조회 2,179
12년 전 조회 1,845
12년 전 조회 1,860
12년 전 조회 1,601
12년 전 조회 2,722
12년 전 조회 1,999
12년 전 조회 1,679
12년 전 조회 2,757
12년 전 조회 1,947
12년 전 조회 2,558
12년 전 조회 3,763
12년 전 조회 1,553
12년 전 조회 2,505
12년 전 조회 1,360
12년 전 조회 1,856
12년 전 조회 2,438
12년 전 조회 2,063
12년 전 조회 4,879
12년 전 조회 2,981
12년 전 조회 3,158
12년 전 조회 1,527
12년 전 조회 8,841
12년 전 조회 3,670
12년 전 조회 1,759
12년 전 조회 1,607
12년 전 조회 3,015
12년 전 조회 5,681
12년 전 조회 3,725