[perf]for 루프 조건의 count() 호출을 루프 밖으로 추출 (핵심 파일)

for ($i=0; $i<count(\$arr); $i++) 패턴은 매 반복마다 count()를 호출하여
불필요한 CPU 오버헤드를 발생시킴. 루프 전에 한 번만 count()를 계산하여
변수에 저장하는 고전적인 최적화.

적용 파일 (19개, 41곳):
- lib/common.lib.php (8)
- common.php (2)
- lib/shop.lib.php (5)
- lib/naverpay.lib.php (1)
- lib/thumbnail.lib.php (2)
- bbs/list.php (1), bbs/search.php (4), bbs/qalist.php (1), bbs/qawrite.php (1)
- bbs/ajax.filter.php (1), bbs/write_update.php (4)
- bbs/write_comment_update.php (1), bbs/memo_form_update.php (2)
- bbs/new_delete.php (1), bbs/move_update.php (4)
- shop/search.php (1), shop/orderform.sub.php (1)
- mobile/shop/search.php (1), mobile/shop/orderform.sub.php (1)

부수 효과:
- lib/shop.lib.php line 2115: 동일 루프 내 strstr($dlcomp, $company)를
  strpos($dlcomp, $company) !== false 로 변경 (strstr 최적화의 일부)

제외:
- theme/, skin/, plugin/ (사용자 커스터마이징/서드파티)
- lib/PHPExcel/ (서드파티 라이브러리)
- adm/ 및 shop/mail/ (관리자/드문 경로, 별도 후속 작업 여지)
- 주석 처리된 코드 (bbs/delete.php:127, bbs/delete_all.php:143)
This commit is contained in:
thisgun
2026-04-16 02:26:59 +00:00
parent 29e55de1fa
commit a612e69d3e
19 changed files with 85 additions and 43 deletions
+2 -1
View File
@@ -7,7 +7,8 @@ $content = isset($_POST['content']) ? strip_tags($_POST['content']) : '';
//$filter = explode(",", strtolower(trim($config['cf_filter'])));
// strtolower 에 의한 한글 변형으로 아래 코드로 대체 (곱슬최씨님이 알려 주셨습니다.)
$filter = explode(",", trim($config['cf_filter']));
for ($i=0; $i<count($filter); $i++) {
$filter_cnt = count($filter);
for ($i=0; $i<$filter_cnt; $i++) {
$str = $filter[$i];
// 제목 필터링 (찾으면 중지)
+2 -1
View File
@@ -14,7 +14,8 @@ if ($board['bo_use_category']) {
$category_option .= '>전체</a></li>';
$categories = explode('|', $board['bo_category_list']); // 구분자가 , 로 되어 있음
for ($i=0; $i<count($categories); $i++) {
$categories_cnt = count($categories);
for ($i=0; $i<$categories_cnt; $i++) {
$category = trim($categories[$i]);
if ($category=='') continue;
$category_option .= '<li><a href="'.(get_pretty_url($bo_table,'','sca='.urlencode($category))).'"';
+4 -2
View File
@@ -18,7 +18,8 @@ $me_memo = isset($_POST['me_memo']) ? preg_replace("#[\\\]+$#", "", substr(trim(
run_event('memo_form_update_before', $recv_list);
for ($i=0; $i<count($recv_list); $i++) {
$recv_list_cnt = count($recv_list);
for ($i=0; $i<$recv_list_cnt; $i++) {
$recv_list_id = substr(preg_replace("/[^a-zA-Z0-9_]*/", "", $recv_list[$i]), 0, 20);
@@ -63,7 +64,8 @@ if (!$is_admin) {
}
}
for ($i=0; $i<count($member_list['id']); $i++) {
$member_list_cnt = count($member_list['id']);
for ($i=0; $i<$member_list_cnt; $i++) {
$tmp_row = sql_fetch(" select max(me_id) as max_me_id from {$g5['memo_table']} ");
$me_id = $tmp_row['max_me_id'] + 1;
+8 -4
View File
@@ -233,10 +233,12 @@ delete_cache_latest($bo_table);
if ($sw == 'move')
{
for ($i=0; $i<count($save); $i++)
$save_cnt = count($save);
for ($i=0; $i<$save_cnt; $i++)
{
if( isset($save[$i]['bf_file']) && $save[$i]['bf_file'] ){
for ($k=0; $k<count($save[$i]['bf_file']); $k++) {
$bf_file_cnt = count($save[$i]['bf_file']);
for ($k=0; $k<$bf_file_cnt; $k++) {
$del_file = run_replace('delete_file_path', clean_relative_paths($save[$i]['bf_file'][$k]), $save[$i]);
if ( is_file($del_file) && file_exists($del_file) ){
@@ -248,7 +250,8 @@ if ($sw == 'move')
}
}
for ($k=0; $k<count($save[$i]['wr_contents']); $k++){
$wr_contents_cnt = count($save[$i]['wr_contents']);
for ($k=0; $k<$wr_contents_cnt; $k++){
delete_editor_thumbnail($save[$i]['wr_contents'][$k]);
}
@@ -262,7 +265,8 @@ if ($sw == 'move')
$sql = " select bo_notice from {$g5['board_table']} where bo_table = '{$bo_table}' ";
$row = sql_fetch($sql);
$arr_notice = explode(',', $row['bo_notice']);
for ($i=0; $i<count($arr_notice); $i++) {
$arr_notice_cnt = count($arr_notice);
for ($i=0; $i<$arr_notice_cnt; $i++) {
$move_id = (int)$arr_notice[$i];
// 게시판에 wr_id 가 있다면 이동한게 아니므로 bo_notice 에 다시 넣음
$row2 = sql_fetch(" select count(*) as cnt from $write_table where wr_id = '{$move_id}' ");
+2 -1
View File
@@ -97,7 +97,8 @@ for($i=0;$i<$count_chk_bn_id;$i++)
$notice_array = explode(",", trim($board['bo_notice']));
$bo_notice = "";
$lf = '';
for ($k=0; $k<count($notice_array); $k++) {
$notice_array_cnt = count($notice_array);
for ($k=0; $k<$notice_array_cnt; $k++) {
if ((int)$write['wr_id'] != (int)$notice_array[$k])
$bo_notice .= $lf.$notice_array[$k];
+2 -1
View File
@@ -29,7 +29,8 @@ if ($qaconfig['qa_category']) {
$category_option .= '>전체</a></li>';
$categories = explode('|', $qaconfig['qa_category']); // 구분자가 | 로 되어 있음
for ($i=0; $i<count($categories); $i++) {
$categories_cnt = count($categories);
for ($i=0; $i<$categories_cnt; $i++) {
$category = trim($categories[$i]);
if ($category=='') continue;
$category_msg = '';
+2 -1
View File
@@ -54,7 +54,8 @@ if(is_file($skin_file)) {
$category_option = '';
if(trim($qaconfig['qa_category'])) {
$category = explode('|', $qaconfig['qa_category']);
for($i=0; $i<count($category); $i++) {
$category_cnt = count($category);
for($i=0; $i<$category_cnt; $i++) {
$category_option .= option_selected($category[$i], $write['qa_category']);
}
} else {
+8 -4
View File
@@ -72,7 +72,9 @@ if ($stx) {
$field = explode('||', trim($sfl));
$str = '(';
for ($i=0; $i<count($s); $i++) {
$s_cnt = count($s);
$field_cnt = count($field);
for ($i=0; $i<$s_cnt; $i++) {
if (trim($s[$i]) == '') continue;
$search_str = $s[$i];
@@ -85,7 +87,7 @@ if ($stx) {
$op2 = '';
// 필드의 수만큼 다중 필드 검색 가능 (필드1+필드2...)
for ($k=0; $k<count($field); $k++) {
for ($k=0; $k<$field_cnt; $k++) {
$str .= $op2;
switch ($field[$k]) {
case 'mb_id' :
@@ -119,7 +121,8 @@ if ($stx) {
$time1 = get_microtime();
$total_count = 0;
for ($i=0; $i<count($g5_search['tables']); $i++) {
$tables_cnt = count($g5_search['tables']);
for ($i=0; $i<$tables_cnt; $i++) {
$tmp_write_table = $g5['write_prefix'] . $g5_search['tables'][$i];
$sql = " select wr_id from {$tmp_write_table} where {$sql_search} ";
@@ -148,7 +151,8 @@ if ($stx) {
if ($page < 1) { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함
for ($i=0; $i<count($search_table); $i++) {
$search_table_cnt = count($search_table);
for ($i=0; $i<$search_table_cnt; $i++) {
if ($from_record < $search_table_count[$i]) {
$table_index = $i;
$from_record = $from_record - ($i > 0 ? $search_table_count[$i-1] : 0);
+2 -1
View File
@@ -256,7 +256,8 @@ if ($w == 'c') // 댓글 입력
// 중복된 메일 주소는 제거
$unique_email = array_unique($array_email);
$unique_email = array_values($unique_email);
for ($i=0; $i<count($unique_email); $i++) {
$unique_email_cnt = count($unique_email);
for ($i=0; $i<$unique_email_cnt; $i++) {
mailer($wr_name, $wr_email, $unique_email[$i], $subject, $content, 1);
}
}
+8 -4
View File
@@ -433,7 +433,8 @@ if ($w == '' || $w == 'r') {
}
} else {
$bo_notice = '';
for ($i=0; $i<count($notice_array); $i++)
$notice_array_cnt = count($notice_array);
for ($i=0; $i<$notice_array_cnt; $i++)
if ((int)$wr_id != (int)$notice_array[$i])
$bo_notice .= $notice_array[$i] . ',';
$bo_notice = trim($bo_notice);
@@ -483,7 +484,8 @@ $file_upload_msg = '';
$upload = array();
if(isset($_FILES['bf_file']['name']) && is_array($_FILES['bf_file']['name'])) {
for ($i=0; $i<count($_FILES['bf_file']['name']); $i++) {
$bf_file_cnt = count($_FILES['bf_file']['name']);
for ($i=0; $i<$bf_file_cnt; $i++) {
$upload[$i]['file'] = '';
$upload[$i]['source'] = '';
$upload[$i]['filesize'] = 0;
@@ -598,7 +600,8 @@ if(isset($_FILES['bf_file']['name']) && is_array($_FILES['bf_file']['name'])) {
} // end if
// 나중에 테이블에 저장하는 이유는 $wr_id 값을 저장해야 하기 때문입니다.
for ($i=0; $i<count($upload); $i++)
$upload_cnt = count($upload);
for ($i=0; $i<$upload_cnt; $i++)
{
$upload[$i]['source'] = sql_real_escape_string($upload[$i]['source']);
$bf_content[$i] = isset($bf_content[$i]) ? sql_real_escape_string($bf_content[$i]) : '';
@@ -754,7 +757,8 @@ if (!($w == 'u' || $w == 'cu') && $config['cf_email_use'] && $board['bo_use_emai
$unique_email = array_unique($array_email);
$unique_email = run_replace('write_update_mail_list', array_values($unique_email), $board, $wr_id);
for ($i=0; $i<count($unique_email); $i++) {
$unique_email_cnt = count($unique_email);
for ($i=0; $i<$unique_email_cnt; $i++) {
mailer($wr_name, $wr_email, $unique_email[$i], $subject, $content, 1);
}
}
+4 -2
View File
@@ -609,7 +609,8 @@ if (!(isset($member['mb_id']) && $config['cf_admin'] === $member['mb_id'])) {
if ($cf_possible_ip) {
$is_possible_ip = false;
$pattern = explode("\n", $cf_possible_ip);
for ($i=0; $i<count($pattern); $i++) {
$pattern_cnt = count($pattern);
for ($i=0; $i<$pattern_cnt; $i++) {
$pattern[$i] = trim($pattern[$i]);
if (empty($pattern[$i]))
continue;
@@ -628,7 +629,8 @@ if (!(isset($member['mb_id']) && $config['cf_admin'] === $member['mb_id'])) {
// 접근차단 IP
$is_intercept_ip = false;
$pattern = explode("\n", trim($config['cf_intercept_ip']));
for ($i=0; $i<count($pattern); $i++) {
$pattern_cnt = count($pattern);
for ($i=0; $i<$pattern_cnt; $i++) {
$pattern[$i] = trim($pattern[$i]);
if (empty($pattern[$i]))
continue;
+16 -8
View File
@@ -572,7 +572,8 @@ function search_font($stx, $str)
// "/(검색1|검색2)/i" 와 같은 패턴을 만듬
$pattern = '';
$bar = '';
for ($m=0; $m<count($s); $m++) {
$s_cnt = count($s);
for ($m=0; $m<$s_cnt; $m++) {
if (trim($s[$m]) == '') continue;
// 태그는 포함하지 않아야 하는데 잘 안되는군. ㅡㅡa
//$pattern .= $bar . '([^<])(' . quotemeta($s[$m]) . ')';
@@ -799,7 +800,9 @@ function get_sql_search($search_ca_name, $search_field, $search_text, $search_op
$not_comment = $tmp[1];
$str .= "(";
for ($i=0; $i<count($s); $i++) {
$s_cnt = count($s);
$field_cnt = count($field);
for ($i=0; $i<$s_cnt; $i++) {
// 검색어
$search_str = trim($s[$i]);
if ($search_str == "") continue;
@@ -811,7 +814,7 @@ function get_sql_search($search_ca_name, $search_field, $search_text, $search_op
$str .= "(";
$op2 = "";
for ($k=0; $k<count($field); $k++) { // 필드의 수만큼 다중 필드 검색 가능 (필드1+필드2...)
for ($k=0; $k<$field_cnt; $k++) { // 필드의 수만큼 다중 필드 검색 가능 (필드1+필드2...)
// SQL Injection 방지
// 필드값에 a-z A-Z 0-9 _ , | 이외의 값이 있다면 검색필드를 wr_subject 로 설정한다.
@@ -1056,7 +1059,8 @@ function get_category_option($bo_table='', $ca_name='')
$categories = explode("|", $board['bo_category_list'].($is_admin?"|공지":"")); // 구분자가 | 로 되어 있음
$str = "";
for ($i=0; $i<count($categories); $i++) {
$categories_cnt = count($categories);
for ($i=0; $i<$categories_cnt; $i++) {
$category = trim($categories[$i]);
if (!$category) continue;
@@ -2889,7 +2893,8 @@ function delete_editor_thumbnail($contents)
if(!$matchs)
return;
for($i=0; $i<count($matchs[1]); $i++) {
$matchs_cnt = count($matchs[1]);
for($i=0; $i<$matchs_cnt; $i++) {
// 이미지 path 구함
$imgurl = @parse_url($matchs[1][$i]);
// $srcfile = dirname(G5_PATH).$imgurl['path'];
@@ -3508,7 +3513,8 @@ function module_exec_check($exe, $type)
break;
}
for($i=0; $i<count($out); $i++) {
$out_cnt = count($out);
for($i=0; $i<$out_cnt; $i++) {
if(strpos($out[$i], 'KCP ENC') !== false) {
$search = true;
break;
@@ -3523,7 +3529,8 @@ function module_exec_check($exe, $type)
break;
}
for($i=0; $i<count($out); $i++) {
$out_cnt = count($out);
for($i=0; $i<$out_cnt; $i++) {
if(strpos($out[$i], 'CLIENT') !== false) {
$search = true;
break;
@@ -3538,7 +3545,8 @@ function module_exec_check($exe, $type)
break;
}
for($i=0; $i<count($out); $i++) {
$out_cnt = count($out);
for($i=0; $i<$out_cnt; $i++) {
if(strpos(strtolower($out[$i]), 'ret code') !== false) {
$search = true;
break;
+2 -1
View File
@@ -89,7 +89,8 @@ class naverpay_register
$send_cost_limit = explode(";", $default['de_send_cost_limit']);
$send_cost_list = explode(";", $default['de_send_cost_list']);
for ($k=0; $k<count($send_cost_limit); $k++) {
$send_cost_limit_cnt = count($send_cost_limit);
for ($k=0; $k<$send_cost_limit_cnt; $k++) {
// 총판매금액이 배송비 상한가 보다 작다면
if ($total_price < preg_replace('/[^0-9]/', '', $send_cost_limit[$k])) {
$diff_cost = preg_replace('/[^0-9]/', '', $send_cost_list[$k]);
+11 -6
View File
@@ -1819,7 +1819,8 @@ function get_sendcost($cart_id, $selected=1)
$send_cost_limit = explode(";", $default['de_send_cost_limit']);
$send_cost_list = explode(";", $default['de_send_cost_list']);
$send_cost = 0;
for ($k=0; $k<count($send_cost_limit); $k++) {
$send_cost_limit_cnt = count($send_cost_limit);
for ($k=0; $k<$send_cost_limit_cnt; $k++) {
// 총판매금액이 배송비 상한가 보다 작다면
if ($total_price < preg_replace('/[^0-9]/', '', $send_cost_limit[$k])) {
$send_cost = preg_replace('/[^0-9]/', '', $send_cost_list[$k]);
@@ -1894,7 +1895,8 @@ function get_item_sendcost2($it_id, $price, $qty)
$send_cost_limit = explode(";", $default['de_send_cost_limit']);
$send_cost_list = explode(";", $default['de_send_cost_list']);
for ($k=0; $k<count($send_cost_limit); $k++) {
$send_cost_limit_cnt = count($send_cost_limit);
for ($k=0; $k<$send_cost_limit_cnt; $k++) {
// 총판매금액이 배송비 상한가 보다 작다면
if ($price < preg_replace('/[^0-9]/', '', $send_cost_limit[$k])) {
$sendcost = preg_replace('/[^0-9]/', '', $send_cost_list[$k]);
@@ -2134,8 +2136,9 @@ function get_delivery_inquiry($company, $invoice, $class='')
$dlcomp = explode(")", str_replace("(", "", G5_DELIVERY_COMPANY));
for($i=0; $i<count($dlcomp); $i++) {
if(strstr($dlcomp[$i], $company)) {
$dlcomp_cnt = count($dlcomp);
for($i=0; $i<$dlcomp_cnt; $i++) {
if(strpos($dlcomp[$i], $company) !== false) {
list($com, $url, $tel) = explode("^", $dlcomp[$i]);
break;
}
@@ -2411,7 +2414,8 @@ function get_delivery_company($company)
$option .= '<option value="자체배송" '.get_selected($company, '자체배송').'>자체배송</option>'.PHP_EOL;
$dlcomp = explode(")", str_replace("(", "", G5_DELIVERY_COMPANY));
for ($i=0; $i<count($dlcomp); $i++) {
$dlcomp_cnt = count($dlcomp);
for ($i=0; $i<$dlcomp_cnt; $i++) {
if (trim($dlcomp[$i])=="") continue;
list($value, $url, $tel) = explode("^", $dlcomp[$i]);
$option .= '<option value="'.$value.'" '.get_selected($company, $value).'>'.$value.'</option>'.PHP_EOL;
@@ -2429,7 +2433,8 @@ function get_itemuse_thumb($contents, $thumb_width, $thumb_height, $is_create=fa
$matches = get_editor_image($contents, false);
for($i=0; $i<count($matches[1]); $i++)
$matches_cnt = count($matches[1]);
for($i=0; $i<$matches_cnt; $i++)
{
// 이미지 path 구함
$p = parse_url($matches[1][$i]);
+4 -2
View File
@@ -29,7 +29,8 @@ function get_list_thumbnail($bo_table, $wr_id, $thumb_width, $thumb_height, $is_
$edt = true;
if( $matches = get_editor_image($write['wr_content'], false) ){
for($i=0; $i<count($matches[1]); $i++)
$matches_cnt = count($matches[1]);
for($i=0; $i<$matches_cnt; $i++)
{
// 이미지 path 구함
$p = parse_url($matches[1][$i]);
@@ -117,7 +118,8 @@ function get_view_thumbnail($contents, $thumb_width=0)
$extensions = array(1=>'gif', 2=>'jpg', 3=>'png', 18=>'webp');
for($i=0; $i<count($matches[1]); $i++) {
$matches_cnt = count($matches[1]);
for($i=0; $i<$matches_cnt; $i++) {
$img = $matches[1][$i];
$img_tag = isset($matches[0][$i]) ? $matches[0][$i] : '';
+2 -1
View File
@@ -753,7 +753,8 @@ if($is_kakaopay_use) {
{
$bank_account = '<select name="od_bank_account" id="od_bank_account">'.PHP_EOL;
$bank_account .= '<option value="">선택하십시오.</option>';
for ($i=0; $i<count($str); $i++)
$str_cnt = count($str);
for ($i=0; $i<$str_cnt; $i++)
{
//$str[$i] = str_replace("\r", "", $str[$i]);
$str[$i] = trim($str[$i]);
+2 -1
View File
@@ -64,7 +64,8 @@ if($search_all) {
if ($q) {
$arr = explode(" ", $q);
$detail_where = array();
for ($i=0; $i<count($arr); $i++) {
$arr_cnt = count($arr);
for ($i=0; $i<$arr_cnt; $i++) {
$word = trim($arr[$i]);
if (!$word) continue;
+2 -1
View File
@@ -769,7 +769,8 @@ if($is_kakaopay_use) {
{
$bank_account = '<select name="od_bank_account" id="od_bank_account">'.PHP_EOL;
$bank_account .= '<option value="">선택하십시오.</option>';
for ($i=0; $i<count($str); $i++)
$str_cnt = count($str);
for ($i=0; $i<$str_cnt; $i++)
{
//$str[$i] = str_replace("\r", "", $str[$i]);
$str[$i] = trim($str[$i]);
+2 -1
View File
@@ -72,7 +72,8 @@ if($search_all) {
if ($q) {
$arr = explode(" ", $q);
$detail_where = array();
for ($i=0; $i<count($arr); $i++) {
$arr_cnt = count($arr);
for ($i=0; $i<$arr_cnt; $i++) {
$word = trim($arr[$i]);
if (!$word) continue;