COMING SOON 🚀

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

· 12년 전 · 3537 · 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,722
12년 전 조회 2,564
12년 전 조회 5,137
12년 전 조회 2,782
12년 전 조회 2,060
12년 전 조회 2,752
12년 전 조회 1,863
12년 전 조회 2,094
12년 전 조회 2,520
12년 전 조회 2,391
12년 전 조회 6,387
12년 전 조회 3,749
12년 전 조회 3,026
12년 전 조회 1,780
12년 전 조회 3,311
12년 전 조회 1,975
12년 전 조회 2,268
12년 전 조회 2,021
12년 전 조회 4,555
12년 전 조회 1,895
12년 전 조회 4,780
12년 전 조회 2,506
12년 전 조회 2,760
12년 전 조회 3,395
12년 전 조회 3,453
12년 전 조회 1,704
12년 전 조회 1,793
12년 전 조회 2,440
12년 전 조회 1,794
12년 전 조회 1,943
12년 전 조회 7,751
12년 전 조회 1,713
12년 전 조회 1,998
12년 전 조회 1,641
12년 전 조회 1,688
12년 전 조회 1,926
12년 전 조회 1,915
12년 전 조회 2,879
12년 전 조회 2,916
12년 전 조회 2,508
12년 전 조회 6,373
12년 전 조회 2,670
12년 전 조회 2,749
12년 전 조회 2,137
12년 전 조회 3,150
12년 전 조회 2,532
12년 전 조회 4,230
12년 전 조회 3,716
12년 전 조회 3,066
12년 전 조회 3,538
12년 전 조회 2,982
12년 전 조회 2,824
12년 전 조회 2,157
12년 전 조회 2,286
12년 전 조회 6,713
12년 전 조회 3,777
12년 전 조회 4,245
12년 전 조회 2,903
12년 전 조회 2,610
12년 전 조회 1,970
12년 전 조회 2,961
12년 전 조회 2,106
12년 전 조회 2,615
12년 전 조회 3,852
12년 전 조회 3,478
12년 전 조회 2,826
12년 전 조회 7,747
12년 전 조회 3,615
12년 전 조회 1,823
12년 전 조회 1,787
12년 전 조회 2,523
12년 전 조회 3,139
12년 전 조회 2,000
12년 전 조회 1,901
12년 전 조회 2,500
12년 전 조회 2,663
12년 전 조회 1,863
12년 전 조회 2,294
12년 전 조회 2,603
12년 전 조회 1,965
12년 전 조회 2,300
12년 전 조회 4,903
12년 전 조회 2,635
12년 전 조회 2,159
12년 전 조회 2,036
12년 전 조회 2,434
12년 전 조회 2,690
12년 전 조회 5,454
12년 전 조회 6,789
12년 전 조회 1,913
12년 전 조회 3,501
12년 전 조회 7,945
12년 전 조회 3,628
12년 전 조회 1.1만
12년 전 조회 1,745
12년 전 조회 1,933
12년 전 조회 2,607
12년 전 조회 2,916
12년 전 조회 2,562
12년 전 조회 3,168