COMING SOON 🚀

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

· 12년 전 · 3532 · 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,719
12년 전 조회 2,562
12년 전 조회 5,135
12년 전 조회 2,777
12년 전 조회 2,056
12년 전 조회 2,748
12년 전 조회 1,859
12년 전 조회 2,091
12년 전 조회 2,515
12년 전 조회 2,389
12년 전 조회 6,386
12년 전 조회 3,746
12년 전 조회 3,018
12년 전 조회 1,775
12년 전 조회 3,307
12년 전 조회 1,972
12년 전 조회 2,263
12년 전 조회 2,016
12년 전 조회 4,553
12년 전 조회 1,892
12년 전 조회 4,777
12년 전 조회 2,501
12년 전 조회 2,755
12년 전 조회 3,392
12년 전 조회 3,448
12년 전 조회 1,701
12년 전 조회 1,790
12년 전 조회 2,436
12년 전 조회 1,785
12년 전 조회 1,940
12년 전 조회 7,750
12년 전 조회 1,706
12년 전 조회 1,994
12년 전 조회 1,638
12년 전 조회 1,682
12년 전 조회 1,920
12년 전 조회 1,909
12년 전 조회 2,871
12년 전 조회 2,911
12년 전 조회 2,505
12년 전 조회 6,370
12년 전 조회 2,664
12년 전 조회 2,740
12년 전 조회 2,130
12년 전 조회 3,141
12년 전 조회 2,528
12년 전 조회 4,225
12년 전 조회 3,705
12년 전 조회 3,064
12년 전 조회 3,533
12년 전 조회 2,979
12년 전 조회 2,820
12년 전 조회 2,152
12년 전 조회 2,285
12년 전 조회 6,708
12년 전 조회 3,772
12년 전 조회 4,241
12년 전 조회 2,896
12년 전 조회 2,607
12년 전 조회 1,963
12년 전 조회 2,957
12년 전 조회 2,102
12년 전 조회 2,613
12년 전 조회 3,846
12년 전 조회 3,476
12년 전 조회 2,824
12년 전 조회 7,742
12년 전 조회 3,610
12년 전 조회 1,818
12년 전 조회 1,784
12년 전 조회 2,517
12년 전 조회 3,137
12년 전 조회 1,996
12년 전 조회 1,897
12년 전 조회 2,497
12년 전 조회 2,657
12년 전 조회 1,859
12년 전 조회 2,290
12년 전 조회 2,596
12년 전 조회 1,962
12년 전 조회 2,293
12년 전 조회 4,901
12년 전 조회 2,627
12년 전 조회 2,156
12년 전 조회 2,030
12년 전 조회 2,429
12년 전 조회 2,687
12년 전 조회 5,451
12년 전 조회 6,786
12년 전 조회 1,907
12년 전 조회 3,498
12년 전 조회 7,938
12년 전 조회 3,623
12년 전 조회 1.1만
12년 전 조회 1,740
12년 전 조회 1,930
12년 전 조회 2,603
12년 전 조회 2,913
12년 전 조회 2,556
12년 전 조회 3,159