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

· 12년 전 · 1505

<?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년 전 조회 3,748
12년 전 조회 8,622
12년 전 조회 4,230
12년 전 조회 3,796
12년 전 조회 1,404
12년 전 조회 1,479
12년 전 조회 3,191
12년 전 조회 4,031
12년 전 조회 4,437
12년 전 조회 1,655
12년 전 조회 1,341
12년 전 조회 927
12년 전 조회 1,027
12년 전 조회 2,763
12년 전 조회 2,779
12년 전 조회 1,305
12년 전 조회 2,841
12년 전 조회 1,325
12년 전 조회 8,506
12년 전 조회 1,555
12년 전 조회 3,817
12년 전 조회 6,431
12년 전 조회 1,613
12년 전 조회 2,875
12년 전 조회 631
12년 전 조회 1,339
12년 전 조회 1,200
12년 전 조회 1,364
12년 전 조회 3,359
12년 전 조회 1,136
12년 전 조회 1,858
12년 전 조회 1,295
12년 전 조회 952
12년 전 조회 1,432
12년 전 조회 2,142
12년 전 조회 1,427
12년 전 조회 1,314
12년 전 조회 1,839
12년 전 조회 1.5만
12년 전 조회 1,108
12년 전 조회 918
12년 전 조회 2,757
12년 전 조회 9,008
12년 전 조회 1,320
12년 전 조회 1,212
12년 전 조회 1,680
12년 전 조회 1,184
12년 전 조회 1,323
12년 전 조회 3,598
12년 전 조회 1,506
12년 전 조회 2,176
12년 전 조회 1,196
12년 전 조회 1,348
12년 전 조회 3,071
12년 전 조회 921
12년 전 조회 1,392
12년 전 조회 1,659
12년 전 조회 2,079
12년 전 조회 2,177
12년 전 조회 2,608
12년 전 조회 1,096
12년 전 조회 1,124
12년 전 조회 4,487
12년 전 조회 1,539
12년 전 조회 1,685
12년 전 조회 5,545
12년 전 조회 3,301
12년 전 조회 2,181
12년 전 조회 1,341
12년 전 조회 3,526
12년 전 조회 2,568
12년 전 조회 2,260
12년 전 조회 1,882
12년 전 조회 1,581
12년 전 조회 1,581
12년 전 조회 1,326
12년 전 조회 2,445
12년 전 조회 1,714
12년 전 조회 1,398
12년 전 조회 2,475
12년 전 조회 1,652
12년 전 조회 2,269
12년 전 조회 3,488
12년 전 조회 1,271
12년 전 조회 2,246
12년 전 조회 1,077
12년 전 조회 1,571
12년 전 조회 2,156
12년 전 조회 1,798
12년 전 조회 4,602
12년 전 조회 2,697
12년 전 조회 2,877
12년 전 조회 1,264
12년 전 조회 8,576
12년 전 조회 3,369
12년 전 조회 1,477
12년 전 조회 1,325
12년 전 조회 2,727
12년 전 조회 5,406
12년 전 조회 3,452
🐛 버그신고