chatgpt 작성 오류 원인을 알 수 있을까요?
본문
아래는 write_update.skin.php 파일에 첨부하여,
wr_2에 사과 또는 배 또는 딸기가 있으면 대형,중형,소형에 따라 값을 구하는 소스코드 입니다.
if,else 문으로 만든 것을 줄여달라고 chatgpt에 요청을 하니 아래처럼 작성해 주었는데,
오류가 납니다. 오류 원인을 알 수 있을까요?
도움 부탁 드립니다. ㅜㅜ
$prices = [
'사과' => [
'대형' => [8000, 9000, 10000],
'중형' => [7500, 8500, 9500],
'소형' => [6200, 7200, 8200],
],
'배' => [
'대형' => [8100, 9100, 11000],
'중형' => [7200, 8200, 9200],
'소형' => [6300, 7300, 8300],
],
'딸기' => [
'대형' => [8400, 9400, 14000],
'중형' => [7700, 8700, 9700],
'소형' => [6800, 7800, 8800],
],
];
$type = null;
foreach (array_keys($prices) as $key) {
if (strpos($wr_2, $key) !== false) {
$type = $key;
break;
}
}
if ($type !== null) {
$size = ($wr_3 == '1개') ? $wr_2 : $wr_3;
foreach (['대형', '중형', '소형'] as $sizeKey) {
if (strpos($size, $sizeKey) !== false) {
if (isset($prices[$type][$sizeKey][$wr_4 - 1])) {
$wr_7 = $prices[$type][$sizeKey][$wr_4 - 1];
}
break;
}
}
}
답변 3
$wr_2와 $wr_3는 문자열, $wr_4는 숫자인지 "타입 확인"하는 것이 최우선인 듯합니다.
gpt에 물의셨나요~, 지피티 친구에게 물었더니,
사용 중인 서버의 PHP 버전이 5.3 이하인지 물어보라네요~
그렇다면, 아래 구문이 지원되지 않아 파싱 오류가 발생한 거랍니다. ㅠ ㅠ
$prices = [
'사과' => [
'대형' => [8000, 9000, 10000],
// ...
],
// ...
];
$prices = array(
'사과' => array(
'대형' => array(8000, 9000, 10000),
// ...
),
// ...
);
※ 오류가 """ unexpected '[' """ 또는 배열 선언 관련 구문 오류라면 PHP 버전을 확인하시고,
단축 배열 구문을 array()로 변경하는 것이 최우선 해결책일 거랍니다.
!-->기존 소스도 주세요.
if ($wr_3 == "1개") {
if (strpos($wr_2, "사과") !== false) {
if (strpos($wr_2, "대형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 8000;
} else if ($wr_4 == 2) {
$wr_7 = 9000;
} else if ($wr_4 == 3) {
$wr_7 = 10000;
}
}else if (strpos($wr_2, "중형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 7500;
} else if ($wr_4 == 2) {
$wr_7 = 8500;
} else if ($wr_4 == 3) {
$wr_7 = 9500;
}
}else if (strpos($wr_2, "소형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 6200;
} else if ($wr_4 == 2) {
$wr_7 = 7200;
} else if ($wr_4 == 3) {
$wr_7 = 8200;
}
}
}else if (strpos($wr_2, "배") !== false) {
if (strpos($wr_2, "대형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 8100;
} else if ($wr_4 == 2) {
$wr_7 = 9100;
} else if ($wr_4 == 3) {
$wr_7 = 11000;
}
}else if (strpos($wr_2, "중형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 7200;
} else if ($wr_4 == 2) {
$wr_7 = 8200;
} else if ($wr_4 == 3) {
$wr_7 = 9200;
}
}else if (strpos($wr_2, "소형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 6300;
} else if ($wr_4 == 2) {
$wr_7 = 7300;
} else if ($wr_4 == 3) {
$wr_7 = 8300;
}
}
}else if (strpos($wr_2, "딸기") !== false) {
if (strpos($wr_2, "대형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 8400;
} else if ($wr_4 == 2) {
$wr_7 = 9400;
} else if ($wr_4 == 3) {
$wr_7 = 14000;
}
}else if (strpos($wr_2, "중형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 7700;
} else if ($wr_4 == 2) {
$wr_7 = 8700;
} else if ($wr_4 == 3) {
$wr_7 = 9700;
}
}else if (strpos($wr_2, "소형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 6800;
} else if ($wr_4 == 2) {
$wr_7 = 7800;
} else if ($wr_4 == 3) {
$wr_7 = 8800;
}
}
} // 끝
}else{
if (strpos($wr_2, "사과") !== false) {
if (strpos($wr_3, "대형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 8000;
} else if ($wr_4 == 2) {
$wr_7 = 9000;
} else if ($wr_4 == 3) {
$wr_7 = 10000;
}
}else if (strpos($wr_3, "중형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 7500;
} else if ($wr_4 == 2) {
$wr_7 = 8500;
} else if ($wr_4 == 3) {
$wr_7 = 9500;
}
}else if (strpos($wr_3, "소형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 6200;
} else if ($wr_4 == 2) {
$wr_7 = 7200;
} else if ($wr_4 == 3) {
$wr_7 = 8200;
}
}
}else if (strpos($wr_2, "배") !== false) {
if (strpos($wr_3, "대형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 8100;
} else if ($wr_4 == 2) {
$wr_7 = 9100;
} else if ($wr_4 == 3) {
$wr_7 = 11000;
}
}else if (strpos($wr_3, "중형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 7200;
} else if ($wr_4 == 2) {
$wr_7 = 8200;
} else if ($wr_4 == 3) {
$wr_7 = 9200;
}
}else if (strpos($wr_3, "소형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 6300;
} else if ($wr_4 == 2) {
$wr_7 = 7300;
} else if ($wr_4 == 3) {
$wr_7 = 8300;
}
}
}else if (strpos($wr_2, "딸기") !== false) {
if (strpos($wr_3, "대형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 8400;
} else if ($wr_4 == 2) {
$wr_7 = 9400;
} else if ($wr_4 == 3) {
$wr_7 = 14000;
}
}else if (strpos($wr_3, "중형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 7700;
} else if ($wr_4 == 2) {
$wr_7 = 8700;
} else if ($wr_4 == 3) {
$wr_7 = 9700;
}
}else if (strpos($wr_3, "소형") !== false) {
if ($wr_4 == 1) {
$wr_7 = 6800;
} else if ($wr_4 == 2) {
$wr_7 = 7800;
} else if ($wr_4 == 3) {
$wr_7 = 8800;
}
}
} // 끝
}
이 if문을 gpt에 요청하였습니다.