Why do comparisons and empty () behave like this in PHP?

PHP:

$a = "0";
$b = "00";
var_dump(empty($a)); # True (wtf?)
var_dump($a == $b); # True... WTF???
var_dump(empty($b)); # False WWWTTTFFFF!!??

I read the docs. But the documents do not explain why they designed it that way . I'm not looking for workarounds (I already know them), I'm looking for explanations.

Why is that? Will this make some things easier?

+3
source share
6 answers

All this stems from the goal of the language developers to "do the right thing."

That this piece of code should do what the niave programmer or an occasional viewer of a piece of code expects from it. It was not an easy task.

Php (, C if (a = b) {... perl, ( "xxxx" == 0) {print "True!";}). 0 == 0000 if ( "000" ) {echo "True!"; } - , , , pracice . , "" === , php .

+1

"0" == "00" true, :

, . , . switch.

( )

"0" "00" , , , 0 == 0.

=== ===, .

empty():

, :

  • "( )
  • 0 (0 )
  • " 0" (0 )
  • NULL
  • FALSE
  • array() ( )
  • var $var; ( , )
+9

http://au2.php.net/empty

: "0" (0 )

"00" .

+2

, PHP , , @Shadow, . 0 00 PHP. :

($ a === $b)// b ()

+1

http://us.php.net/empty. .

, PHP . , . , , , int .

0

From the documentation, it can be assumed that 0 can be either int or 1 char to indicate empty. 00 is more likely a formatting assumption, since there is no such thing as 00, but there is 0. 00 will mean 2 integer formats, but the empty () function is written only for 0.

Developer FWIW IANA php.

0
source

Source: https://habr.com/ru/post/1717596/


All Articles