COMING SOON 🚀

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

· 12년 전 · 3545 · 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,732
12년 전 조회 2,573
12년 전 조회 5,144
12년 전 조회 2,787
12년 전 조회 2,062
12년 전 조회 2,755
12년 전 조회 1,868
12년 전 조회 2,099
12년 전 조회 2,528
12년 전 조회 2,398
12년 전 조회 6,396
12년 전 조회 3,755
12년 전 조회 3,033
12년 전 조회 1,787
12년 전 조회 3,318
12년 전 조회 1,980
12년 전 조회 2,274
12년 전 조회 2,029
12년 전 조회 4,562
12년 전 조회 1,900
12년 전 조회 4,788
12년 전 조회 2,515
12년 전 조회 2,763
12년 전 조회 3,407
12년 전 조회 3,459
12년 전 조회 1,709
12년 전 조회 1,797
12년 전 조회 2,453
12년 전 조회 1,801
12년 전 조회 1,949
12년 전 조회 7,757
12년 전 조회 1,722
12년 전 조회 2,005
12년 전 조회 1,648
12년 전 조회 1,694
12년 전 조회 1,929
12년 전 조회 1,923
12년 전 조회 2,881
12년 전 조회 2,922
12년 전 조회 2,515
12년 전 조회 6,380
12년 전 조회 2,675
12년 전 조회 2,757
12년 전 조회 2,142
12년 전 조회 3,151
12년 전 조회 2,538
12년 전 조회 4,235
12년 전 조회 3,720
12년 전 조회 3,075
12년 전 조회 3,546
12년 전 조회 2,988
12년 전 조회 2,827
12년 전 조회 2,162
12년 전 조회 2,292
12년 전 조회 6,717
12년 전 조회 3,784
12년 전 조회 4,253
12년 전 조회 2,912
12년 전 조회 2,615
12년 전 조회 1,980
12년 전 조회 2,972
12년 전 조회 2,110
12년 전 조회 2,621
12년 전 조회 3,858
12년 전 조회 3,479
12년 전 조회 2,835
12년 전 조회 7,755
12년 전 조회 3,618
12년 전 조회 1,833
12년 전 조회 1,794
12년 전 조회 2,526
12년 전 조회 3,142
12년 전 조회 2,008
12년 전 조회 1,908
12년 전 조회 2,506
12년 전 조회 2,676
12년 전 조회 1,871
12년 전 조회 2,298
12년 전 조회 2,614
12년 전 조회 1,971
12년 전 조회 2,305
12년 전 조회 4,912
12년 전 조회 2,643
12년 전 조회 2,163
12년 전 조회 2,042
12년 전 조회 2,442
12년 전 조회 2,693
12년 전 조회 5,458
12년 전 조회 6,798
12년 전 조회 1,921
12년 전 조회 3,510
12년 전 조회 7,951
12년 전 조회 3,635
12년 전 조회 1.1만
12년 전 조회 1,749
12년 전 조회 1,943
12년 전 조회 2,616
12년 전 조회 2,923
12년 전 조회 2,567
12년 전 조회 3,189