라이브러리
[PHP] max - 가장 높은 값 찾기
PHP의 max() 함수
PHP의 `max()` 함수는 배열이나 인수 목록에서 가장 큰 값을 반환합니다.
# 기본 사용법
#hostingforum.kr
php
$max = max(1, 2, 3, 4, 5);
echo $max; // 5
# 배열 사용
#hostingforum.kr
php
$numbers = array(1, 2, 3, 4, 5);
$max = max($numbers);
echo $max; // 5
# 여러 배열 사용
#hostingforum.kr
php
$numbers1 = array(1, 2, 3);
$numbers2 = array(4, 5, 6);
$max = max($numbers1, $numbers2);
echo $max; // 6
# 문자열 사용
#hostingforum.kr
php
$strings = array('apple', 'banana', 'cherry');
$max = max($strings);
echo $max; // 'cherry'
# 정렬 사용
#hostingforum.kr
php
$numbers = array(1, 2, 3, 4, 5);
rsort($numbers);
$max = max($numbers);
echo $max; // 5
# 정렬 후 max() 사용
#hostingforum.kr
php
$numbers = array(1, 2, 3, 4, 5);
sort($numbers);
$max = max($numbers);
echo $max; // 5
# 중복된 값이 있는 경우
#hostingforum.kr
php
$numbers = array(1, 2, 2, 3, 4);
$max = max($numbers);
echo $max; // 4
# 중복된 값이 있는 경우 정렬 후 max() 사용
#hostingforum.kr
php
$numbers = array(1, 2, 2, 3, 4);
rsort($numbers);
$max = max($numbers);
echo $max; // 4
PHP의 max() 함수와 min() 함수의 차이
PHP의 `max()` 함수와 `min()` 함수는 모두 배열이나 인수 목록에서 가장 큰/작은 값을 반환합니다.
# min() 함수
#hostingforum.kr
php
$numbers = array(1, 2, 3, 4, 5);
$min = min($numbers);
echo $min; // 1
결론
PHP의 `max()` 함수는 배열이나 인수 목록에서 가장 큰 값을 반환합니다. 이 함수는 다양한 사용법이 가능하며, 정렬 후 max() 사용, 중복된 값이 있는 경우, 문자열 사용 등 다양한 예제가 있습니다.
댓글목록
등록된 댓글이 없습니다.