COMING SOON 🚀

php변수를 스크립트로 사용하는 방법

· 12년 전 · 3561 · 2
<?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) 17회 2013-08-22 21:11
|

댓글 2개

감사해요 ㅎㅎ
유용한 정보네요
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
12년 전 조회 2,746
12년 전 조회 2,595
12년 전 조회 5,164
12년 전 조회 2,800
12년 전 조회 2,079
12년 전 조회 2,775
12년 전 조회 1,887
12년 전 조회 2,123
12년 전 조회 2,548
12년 전 조회 2,417
12년 전 조회 6,412
12년 전 조회 3,768
12년 전 조회 3,055
12년 전 조회 1,802
12년 전 조회 3,337
12년 전 조회 1,997
12년 전 조회 2,287
12년 전 조회 2,045
12년 전 조회 4,578
12년 전 조회 1,918
12년 전 조회 4,813
12년 전 조회 2,533
12년 전 조회 2,779
12년 전 조회 3,419
12년 전 조회 3,483
12년 전 조회 1,726
12년 전 조회 1,818
12년 전 조회 2,473
12년 전 조회 1,820
12년 전 조회 1,967
12년 전 조회 7,770
12년 전 조회 1,743
12년 전 조회 2,025
12년 전 조회 1,666
12년 전 조회 1,712
12년 전 조회 1,949
12년 전 조회 1,939
12년 전 조회 2,905
12년 전 조회 2,944
12년 전 조회 2,533
12년 전 조회 6,396
12년 전 조회 2,692
12년 전 조회 2,775
12년 전 조회 2,159
12년 전 조회 3,166
12년 전 조회 2,558
12년 전 조회 4,252
12년 전 조회 3,731
12년 전 조회 3,085
12년 전 조회 3,562
12년 전 조회 3,009
12년 전 조회 2,841
12년 전 조회 2,178
12년 전 조회 2,311
12년 전 조회 6,736
12년 전 조회 3,802
12년 전 조회 4,274
12년 전 조회 2,927
12년 전 조회 2,630
12년 전 조회 2,003
12년 전 조회 2,988
12년 전 조회 2,128
12년 전 조회 2,640
12년 전 조회 3,879
12년 전 조회 3,498
12년 전 조회 2,854
12년 전 조회 7,774
12년 전 조회 3,632
12년 전 조회 1,845
12년 전 조회 1,818
12년 전 조회 2,547
12년 전 조회 3,171
12년 전 조회 2,026
12년 전 조회 1,925
12년 전 조회 2,526
12년 전 조회 2,699
12년 전 조회 1,890
12년 전 조회 2,319
12년 전 조회 2,637
12년 전 조회 1,992
12년 전 조회 2,334
12년 전 조회 4,928
12년 전 조회 2,670
12년 전 조회 2,180
12년 전 조회 2,061
12년 전 조회 2,459
12년 전 조회 2,705
12년 전 조회 5,479
12년 전 조회 6,822
12년 전 조회 1,945
12년 전 조회 3,532
12년 전 조회 7,977
12년 전 조회 3,652
12년 전 조회 1.1만
12년 전 조회 1,769
12년 전 조회 1,967
12년 전 조회 2,648
12년 전 조회 2,944
12년 전 조회 2,582
12년 전 조회 3,210