Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Why empty('0') = true; empty('00') = false;

I eval $var using

if(empty($_GET['var'])){
    ...
    }

I take TRUE from

https://myweb.com/?var=0

I take FALSE from

https://myweb.com/?var=00
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The empty pseudo-function shares its logic with casting to boolean - if something is equivalent to "false", it is considered "empty".

The list of values which are considered "empty" is intended to be helpful, but is occasionally confusing, because there isn't really one perfect answer. Starting off with integers, it seems reasonable that 0 is "empty", but for instance 1 is not. Because user input almost always comes in the form of strings (particularly on the web, where PHP is most at home), it's also useful for the string "0" to behave the same as the integer 0.

On the face of it, "00" should also be equivalent to 0, and therefore "empty", but now things start getting messy: if you convert the string "hello" to an integer, that is also 0, so is "hello" also empty? That wouldn't be very useful.

The truth is, casts such as this can only really work one of two ways:

  • Throw an error on any conversion which is not 100% unambiguous.
  • Pick a set of compromises which is mostly useful, but not entirely consistent.

PHP picked the second route, and the difference between empty("0") and empty("00") is one of the side effects of the particular compromise chosen. Other languages which took a similar route (e.g. Perl, JavaScript) have different compromises, with different surprising outcomes.

See also my answer to a similar question here.

over 4 years ago · Santiago Trujillo Relatório

0

Because 0 gives empty in PHP if you check without type, but I think 00 type-cast to a string value and thus it's not empty.

You can strict check with type via the operator ===

over 4 years ago · Santiago Trujillo Relatório

0

I think it's probably because you are receiving the 00 as string value in your code. Because 0 and 00 are both considered as empty by the empty function in PHP. Try executing the code below in one of your PHP's to understand better. You can even check out the sandbox link.

<?php
$a = 0;
$b = 00;
$c = '00';

if(empty($a)) echo 'empty a'; 
if(empty($b)) echo 'empty b';
if(empty($c)) echo 'empty c';

?>

Check the documentation of empty() function, there are a list of values that are considered as empty by it.

The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty:

0
0.0
"0"
""
NULL
FALSE
array()

Additionally, when you are dealing with numeric check, you should always typecast the value to integer and then compare if it's 0 or not. This would be the ideal approach.

<?php
$a = 0;
$b = '0';
$c = '00';
$d = 1;
$e = '1';

if(!intval($a)) echo 'empty a '; 
if(!intval($b)) echo 'empty b ';
if(!intval($c)) echo 'empty c ';
if(!intval($d)) echo 'empty d ';
if(!intval($e)) echo 'empty e ';

?>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda