Study/WebHacking

[PHP] Type Juggling ์ทจ์•ฝ์ 

mnzy๐ŸŒฑ 2024. 6. 20. 04:36

 

PHP๋Š” ๋น„๊ต ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด == ์—ฐ์‚ฐ์ž์™€  === ์—ฐ์‚ฐ์ž๋ฅผ ์ง€์›ํ•œ๋‹ค.

์ด๋•Œ, PHP๋Š” ๋‘ ๊ฐ’์„ ๋น„๊ตํ•˜๊ธฐ ์ „์— ํ•„์š”์— ๋”ฐ๋ผ ํƒ€์ž…์„ ๋ณ€ํ™˜ํ•œ๋‹ค.

์ด๋Ÿฌํ•œ ๋ณ€ํ™˜์€ ์˜๋„ํ•˜์ง€ ์•Š์€ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•˜์—ฌ ๋ณด์•ˆ ์ทจ์•ฝ์ ์„ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

 loose (๋А์Šจํ•œ) ๋น„๊ต ==     vs    strict (์—„๊ฒฉํ•œ) ๋น„๊ต === 

  • ==: ๋‘ ๊ฐ’์˜ ํƒ€์ž…์ด ๋‹ค๋ฅด๋ฉด ํƒ€์ž…์„ ๋ณ€ํ™˜ํ•œ ํ›„ ๋น„๊ต (์ฆ‰, ๊ฐ’๋งŒ ๋น„๊ต)
  • ===: ๋‘ ๊ฐ’์˜ ํƒ€์ž…๊ณผ ๊ฐ’ ๋ชจ๋‘ ๊ฐ™์•„์•ผ TRUE ๋ฆฌํ„ด 

๊ธฐ๋ณธ ์˜ˆ์ œ 

var_dump(0 == '0'); // true
var_dump(0 == '0.0'); // true
var_dump(0 == ''); // true
var_dump(0 == null); // true
var_dump(0 == 'string'); // true (PHP 8.0 ์ด์ „)

 

์ˆซ์ž 0์€ ๋ฌธ์ž์—ด 0 ๊ณผ ๋А์Šจํ•œ ๋น„๊ต ๊ฒฐ๊ณผ, TRUE์ด๋‹ค. 

์ˆซ์ž 0์€ null๊ณผ ๋А์Šจํ•œ ๋น„๊ต ๊ฒฐ๊ณผ, TRUE์ด๋‹ค. 

 

https://www.php.net/manual/en/types.comparisons.php

 

PHP: PHP type comparison tables - Manual

Some function to write out your own comparisson table in tsv format. Can be easily modified to add more testcases and/or binary functions. It will test all comparables against each other with all functions. '==', 'ne' => '!=', 'gt' => '>', 'lt' => '<', 'ne

www.php.net

 

PHP ๊ณต์‹ ๋ฌธ์„œ์— ๊ฐ ์ƒํ™ฉ์— ๋”ฐ๋ฅธ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ์ •๋ฆฌ๋˜์–ด์žˆ๋‹ค.