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

· 12년 전 · 1744

<?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,979
12년 전 조회 8,862
12년 전 조회 4,443
12년 전 조회 4,038
12년 전 조회 1,653
12년 전 조회 1,709
12년 전 조회 3,436
12년 전 조회 4,263
12년 전 조회 4,671
12년 전 조회 1,906
12년 전 조회 1,581
12년 전 조회 1,166
12년 전 조회 1,261
12년 전 조회 3,005
12년 전 조회 3,024
12년 전 조회 1,534
12년 전 조회 3,089
12년 전 조회 1,554
12년 전 조회 8,742
12년 전 조회 1,806
12년 전 조회 4,050
12년 전 조회 6,681
12년 전 조회 1,854
12년 전 조회 3,135
12년 전 조회 886
12년 전 조회 1,571
12년 전 조회 1,426
12년 전 조회 1,588
12년 전 조회 3,582
12년 전 조회 1,373
12년 전 조회 2,090
12년 전 조회 1,541
12년 전 조회 1,197
12년 전 조회 1,670
12년 전 조회 2,396
12년 전 조회 1,661
12년 전 조회 1,570
12년 전 조회 2,080
12년 전 조회 1.5만
12년 전 조회 1,344
12년 전 조회 1,157
12년 전 조회 2,994
12년 전 조회 9,234
12년 전 조회 1,537
12년 전 조회 1,466
12년 전 조회 1,913
12년 전 조회 1,415
12년 전 조회 1,561
12년 전 조회 3,850
12년 전 조회 1,745
12년 전 조회 2,411
12년 전 조회 1,445
12년 전 조회 1,587
12년 전 조회 3,299
12년 전 조회 1,153
12년 전 조회 1,609
12년 전 조회 1,883
12년 전 조회 2,306
12년 전 조회 2,425
12년 전 조회 2,851
12년 전 조회 1,316
12년 전 조회 1,334
12년 전 조회 4,737
12년 전 조회 1,770
12년 전 조회 1,917
12년 전 조회 5,771
12년 전 조회 3,512
12년 전 조회 2,420
12년 전 조회 1,572
12년 전 조회 3,760
12년 전 조회 2,784
12년 전 조회 2,487
12년 전 조회 2,126
12년 전 조회 1,809
12년 전 조회 1,807
12년 전 조회 1,565
12년 전 조회 2,685
12년 전 조회 1,959
12년 전 조회 1,639
12년 전 조회 2,718
12년 전 조회 1,900
12년 전 조회 2,521
12년 전 조회 3,731
12년 전 조회 1,517
12년 전 조회 2,475
12년 전 조회 1,330
12년 전 조회 1,820
12년 전 조회 2,398
12년 전 조회 2,022
12년 전 조회 4,835
12년 전 조회 2,943
12년 전 조회 3,117
12년 전 조회 1,480
12년 전 조회 8,801
12년 전 조회 3,630
12년 전 조회 1,714
12년 전 조회 1,579
12년 전 조회 2,969
12년 전 조회 5,643
12년 전 조회 3,687