PHP does not print output when equal to 0

Asked

Viewed 63 times

0

All results are printed on the screen except when it is 0 and I need that number 0 be printed. If it is NULL it satisfies the condition below and correctly prints the hyphen.

if($dados['dado_1']===null){
  echo "<td width='11%' style='text-align:center;'><p><br/>-</p></td>";
}else{
  echo "<td width='11%' style='text-align:center;><p><br/>".$dados['dado_1']."</p></td>";
}
  • 3

    such verification === is for data only BOOLEAN, with null won’t work.

  • Oops, thanks, good to know. But even switching to == does not solve the problem.

  • The data is of the whole type?

  • No, they’re like FLOAT

2 answers

2

The right thing to do in this case is to use the is_null, because when the variable value is 0 it will return phony, so you won’t fall for the first IF. My suggested code:

if(is_null($dados['dado_1'])){
  echo "<td width='11%' style='text-align:center;'><p><br/>-</p></td>";
}else{
  echo "<td width='11%' style='text-align:center;><p><br/>".(string)$dados['dado_1']."</p></td>";
}
  • Beauty! I will switch to is_null, but anyway even switching to is_null still does not display the resulting when it is '0'

  • You then have to indicate that it is a string. I am changing the answer so you can see how it does.

  • still does not display when the value is 0

1

It must be coming empty, put it like this:

echo "<td width='11%' style='text-align:center;><p><br/>". $dados['dado_1'] ? $dados['dado_1'] : '0' ."</p></td>";
  • Oh my dear, I tended the way you said but it still didn’t work. There was no change in the result. But anyway, could you explain this syntax?

  • 1

    This syntax is an "if inline", basically I’m checking if $data['dados_1'] has true value, if yes it puts the value itself, if it doesn’t put '0'. you already tried to give a var_dump in $dados['dado_1'] when he comes 0?

  • in var_dump all results are displayed including those that have the value equal to 0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.