PHP strcmp()

1 개요[ | ]

PHP strcmp()
  • "바이너리 안전 문자열 비교"
echo strcmp("aa","aa").PHP_EOL;
echo strcmp("aa","bb").PHP_EOL;
echo strcmp("bb","aa").PHP_EOL;
# 0
# -1
# 1
"a" - "a" = 97 - 97 = 0
"a" - "b" = 97 - 98 = -1
"b" - "a" = 98 - 97 = 1
echo strcmp("hello","hello").PHP_EOL;
echo strcmp("hello","world").PHP_EOL;
echo strcmp("world","hello").PHP_EOL;
# 0
# -15
# 15
"h" - "h" = 104 - 104 = 0
"h" - "w" = 104 - 119 = -15
"w" - "h" = 119 - 104 = 15
echo strcmp("aa","ab").PHP_EOL;
echo strcmp("ab","aa").PHP_EOL;
echo strcmp("aa","ac").PHP_EOL;
echo strcmp("ac","aa").PHP_EOL;
# -256
# 256
256 × ( "a" - "b" ) = -256
256 × ( "b" - "a" ) = 256
256 × ( "a" - "c" ) = -512
256 × ( "c" - "a" ) = 512
echo strcmp("aaa","aac").PHP_EOL;
echo strcmp("aac","aaa").PHP_EOL;
echo strcmp("aaaa","aaac").PHP_EOL;
echo strcmp("aaac","aaaa").PHP_EOL;
# -2
# 2
# -512
# 512
"a" - "c" = -2
"c" - "a" = 2
256 × ( "a" - "c" ) = -512
256 × ( "c" - "a" ) = 512

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}