COMING SOON 🚀

PHP 변수를 스크립트로 사용하는 방법입니다.

· 12년 전 · 2539

<?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) 11회 2013-03-20 13:27
|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
12년 전 조회 4,882
12년 전 조회 9,750
12년 전 조회 5,345
12년 전 조회 4,933
12년 전 조회 2,536
12년 전 조회 2,618
12년 전 조회 4,332
12년 전 조회 5,176
12년 전 조회 5,575
12년 전 조회 2,788
12년 전 조회 2,494
12년 전 조회 2,104
12년 전 조회 2,150
12년 전 조회 3,942
12년 전 조회 3,916
12년 전 조회 2,475
12년 전 조회 3,996
12년 전 조회 2,486
12년 전 조회 9,671
12년 전 조회 2,753
12년 전 조회 4,972
12년 전 조회 7,576
12년 전 조회 2,761
12년 전 조회 4,053
12년 전 조회 1,760
12년 전 조회 2,451
12년 전 조회 2,324
12년 전 조회 2,499
12년 전 조회 4,505
12년 전 조회 2,276
12년 전 조회 3,015
12년 전 조회 2,434
12년 전 조회 2,113
12년 전 조회 2,554
12년 전 조회 3,264
12년 전 조회 2,533
12년 전 조회 2,336
12년 전 조회 2,868
12년 전 조회 1.6만
12년 전 조회 2,137
12년 전 조회 1,956
12년 전 조회 3,756
12년 전 조회 1만
12년 전 조회 2,306
12년 전 조회 2,259
12년 전 조회 2,706
12년 전 조회 2,176
12년 전 조회 2,326
12년 전 조회 4,591
12년 전 조회 2,540
12년 전 조회 3,198
12년 전 조회 2,203
12년 전 조회 2,366
12년 전 조회 4,090
12년 전 조회 1,929
12년 전 조회 2,421
12년 전 조회 2,741
12년 전 조회 3,189
12년 전 조회 3,280
12년 전 조회 3,761
12년 전 조회 2,197
12년 전 조회 2,219
12년 전 조회 5,602
12년 전 조회 2,661
12년 전 조회 2,838
12년 전 조회 6,680
12년 전 조회 4,408
12년 전 조회 3,325
13년 전 조회 2,506
13년 전 조회 4,680
13년 전 조회 3,676
13년 전 조회 3,372
13년 전 조회 3,024
13년 전 조회 2,715
13년 전 조회 2,712
13년 전 조회 2,458
13년 전 조회 3,562
13년 전 조회 2,869
13년 전 조회 2,524
13년 전 조회 3,591
13년 전 조회 2,789
13년 전 조회 3,391
13년 전 조회 4,615
13년 전 조회 2,385
13년 전 조회 3,381
13년 전 조회 2,200
13년 전 조회 2,677
13년 전 조회 3,307
13년 전 조회 2,949
13년 전 조회 5,734
13년 전 조회 3,858
13년 전 조회 4,058
13년 전 조회 2,395
13년 전 조회 9,714
13년 전 조회 4,561
13년 전 조회 2,614
13년 전 조회 2,482
13년 전 조회 3,880
13년 전 조회 6,595
13년 전 조회 4,606