<?php
// PHP변수 스크립트에서 쉽게 사용하기
function setScriptVar($name, $obj) {
global $script;
$script = "<script language='javascript'>";
// 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;
}
$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."';";
}
}
}
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);
$market = "I Like Fruit!";
setScriptVar("market", $market);
// 배열
$fruit = array('Apple','Pineapple',array('Tomato','Banana'));
setScriptVar("fruit", $fruit);
$fruit = array('Apple','Pineapple',array('Tomato','Banana'));
setScriptVar("fruit", $fruit);
// 오브젝트와 배열
$list->like = 'Pineapple';
$list->hate = 'Tomato';
$list->buy = array('Apple','Pineapple');
setScriptVar("list", $list);
$list->like = 'Pineapple';
$list->hate = 'Tomato';
$list->buy = array('Apple','Pineapple');
setScriptVar("list", $list);
// 변수타입
$num = array(100,'200');
setScriptVar("num",$num);
?>
$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>
//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>
댓글 2개
12년 전
감사해요 ㅎㅎ
takumi22
12년 전
유용한 정보네요
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 230 | 20년 전 | 2037 | ||
| 229 | 20년 전 | 3131 | ||
| 228 | 20년 전 | 3322 | ||
| 227 | 20년 전 | 2383 | ||
| 226 | 20년 전 | 5458 | ||
| 225 | 20년 전 | 2506 | ||
| 224 | 20년 전 | 2951 | ||
| 223 | 20년 전 | 4195 | ||
| 222 | 20년 전 | 2595 | ||
| 221 | 20년 전 | 2277 | ||
| 220 | 20년 전 | 3665 | ||
| 219 | 20년 전 | 2062 | ||
| 218 | 20년 전 | 3552 | ||
| 217 | 20년 전 | 2468 | ||
| 216 | 20년 전 | 2895 | ||
| 215 | 20년 전 | 2204 | ||
| 214 | 20년 전 | 3324 | ||
| 213 | 20년 전 | 2909 | ||
| 212 | 20년 전 | 3038 | ||
| 211 | 20년 전 | 2137 | ||
| 210 | 20년 전 | 1868 | ||
| 209 | 20년 전 | 2332 | ||
| 208 | 20년 전 | 1961 | ||
| 207 | 20년 전 | 1662 | ||
| 206 | 20년 전 | 1848 | ||
| 205 | 20년 전 | 3937 | ||
| 204 | 20년 전 | 1667 | ||
| 203 | 20년 전 | 2000 | ||
| 202 | 20년 전 | 2339 | ||
| 201 | 20년 전 | 1813 | ||
| 200 | 20년 전 | 2942 | ||
| 199 | 20년 전 | 1997 | ||
| 198 | 20년 전 | 2077 | ||
| 197 | 20년 전 | 3641 | ||
| 196 | 20년 전 | 2962 | ||
| 195 | 20년 전 | 2084 | ||
| 194 | 20년 전 | 10227 | ||
| 193 | 20년 전 | 2235 | ||
| 192 | 20년 전 | 1579 | ||
| 191 | 20년 전 | 2648 | ||
| 190 | 20년 전 | 2280 | ||
| 189 | 20년 전 | 1671 | ||
| 188 | 20년 전 | 1463 | ||
| 187 | 20년 전 | 1894 | ||
| 186 | 20년 전 | 1692 | ||
| 185 | 20년 전 | 1726 | ||
| 184 | 20년 전 | 2326 | ||
| 183 | 20년 전 | 1538 | ||
| 182 | 20년 전 | 1473 | ||
| 181 | 20년 전 | 1612 | ||
| 180 | 20년 전 | 2708 | ||
| 179 | 20년 전 | 1786 | ||
| 178 | 20년 전 | 1841 | ||
| 177 | 20년 전 | 1967 | ||
| 176 | 20년 전 | 1790 | ||
| 175 | 20년 전 | 1862 | ||
| 174 | 20년 전 | 1684 | ||
| 173 | 20년 전 | 2053 | ||
| 172 | 20년 전 | 1772 | ||
| 171 | 20년 전 | 2550 | ||
| 170 | 20년 전 | 2257 | ||
| 169 | 20년 전 | 2536 | ||
| 168 | 20년 전 | 1464 | ||
| 167 | 20년 전 | 1564 | ||
| 166 | 20년 전 | 2138 | ||
| 165 | 20년 전 | 1602 | ||
| 164 | 20년 전 | 3747 | ||
| 163 | 20년 전 | 2637 | ||
| 162 | 20년 전 | 2062 | ||
| 161 | 20년 전 | 2767 | ||
| 160 | 20년 전 | 1713 | ||
| 159 | 20년 전 | 1590 | ||
| 158 | 20년 전 | 2542 | ||
| 157 | 20년 전 | 1468 | ||
| 156 | 20년 전 | 1712 | ||
| 155 | 20년 전 | 3218 | ||
| 154 | 20년 전 | 1875 | ||
| 153 | 20년 전 | 1605 | ||
| 152 | 20년 전 | 4931 | ||
| 151 | 20년 전 | 4560 | ||
| 150 | 20년 전 | 3498 | ||
| 149 | 20년 전 | 3770 | ||
| 148 | 20년 전 | 7050 | ||
| 147 | 20년 전 | 3532 | ||
| 146 | 20년 전 | 2598 | ||
| 145 | 20년 전 | 2597 | ||
| 144 | 20년 전 | 7155 | ||
| 143 | 20년 전 | 4581 | ||
| 142 | 20년 전 | 1885 | ||
| 141 | 20년 전 | 3242 | ||
| 140 | 20년 전 | 1941 | ||
| 139 | 20년 전 | 1535 | ||
| 138 | 20년 전 | 2287 | ||
| 137 | 20년 전 | 1766 | ||
| 136 | 20년 전 | 1442 | ||
| 135 | 20년 전 | 1778 | ||
| 134 | 20년 전 | 2953 | ||
| 133 | 20년 전 | 2427 | ||
| 132 | 20년 전 | 1702 | ||
| 131 | 20년 전 | 1629 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기