Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- proxy
- 문자열
- require_once
- 배열
- array_map
- recursive acronym
- nvm
- 재귀약자
- node.js
- 개발환경
- include
- php
- ipaddress
- include_once
- require
- phpinfo
- 터미널
- trim
- WSL2
- LTRIM
- hyper
- str_replace
- RTRIM
- npm
- 몫과나머지
- 함수
- explode
- array
- implode
- 프록시
Archives
- Today
- Total
PHPINFO
PHP 몫과 나머지 구하기 본문
반응형
PHP에서 몫과 나머지를 구하는 몇 가지 방법을 소개합니다.
$b를 $a로 나누었을 때의 몫과 나머지
<?php
# $b를 $a로 나누었을 때의 몫과 나머지 구하기
//몫 구하는 방법 1
$quotient = ($b - ($b % $a)) / $a;
//몫 구하는 방법 2
$quotient = sprintf('%d', $b / $a);
// 나머지 구하는 방법
$remainder = $b % $a;
함수로 간단히 몫과 나머지 구하는 예제
<?php
function getQuotientAndRemainder($divisor, $dividend) {
$quotient = (int)($divisor / $dividend);
$remainder = $divisor % $dividend;
return array( $quotient, $remainder );
}
list($quotient, $remainder) = getQuotientAndRemainder(10, 3);
더보기
참고
How to find remainder and quotient in php?
몫과 나머지 영어로
몫 : quotient
나머지 : remainder
Comments