Doubt about === and !=

Asked

Viewed 112 times

1

I have a question about showing data only if it’s filled out. I’ve used the === and the !=, but none of them are working.

 <p>
 <div class="columna7"> '.$exibe["Nome1"].'</div>
 <div class="columna7"> '.$exibe["DataNascimento"].'</div>
 <div class="columna7">' .(date('Y/m/d') - $exibe["DataNascimento"] ).'</div>
 <div class="columna7"> '.$exibe["Funcao1"].'</div>
 <div class="columna7"> '.$exibe["EPISValidade"].'</div>
 <div class="columna7"> '.$exibe["MedicaValidade"].'</div>
 <div class="columna7"> <a href="TrabalhadorMostrar1.php?id='.$exibe['id'].'"> Ver Documentos </a></div>
 </div>


    ';
        if ($exibe['Nome1'] === '') {

 }

    '<p>
 <div class="columna7"> '.$exibe["Nome1"].'</div>
 <div class="columna7"> '.$exibe["DataNascimento"].'</div>
 <div class="columna7">' .(date('Y/m/d') - $exibe["DataNascimento"] ).'</div>
 <div class="columna7"> '.$exibe["Funcao1"].'</div>
 <div class="columna7"> '.$exibe["EPISValidade"].'</div>
 <div class="columna7"> '.$exibe["MedicaValidade"].'</div>
 <div class="columna7"> <a href="TrabalhadorMostrar1.php?id='.$exibe['id'].'"> Ver Documentos </a></div>
 </div>';

/////

        if($exibe['Nome3'] != NULL) {
        '<p>
 <div class="columna7"> '.$exibe["Nome1"].'</div>
 <div class="columna7"> '.$exibe["DataNascimento"].'</div>
 <div class="columna7">' .(date('Y/m/d') - $exibe["DataNascimento"] ).'</div>
 <div class="columna7"> '.$exibe["Funcao1"].'</div>
 <div class="columna7"> '.$exibe["EPISValidade"].'</div>
 <div class="columna7"> '.$exibe["MedicaValidade"].'</div>
 <div class="columna7"> <a href="TrabalhadorMostrar1.php?id='.$exibe['id'].'"> Ver Documentos </a></div>
  </div>';}

The first shows. The other two exist and does not show me the data.

  • 1

    === Compares the tipo data(string, integer etc) and the valor, already != checks only if the value is different. operators

3 answers

4


use the method is_null()

 if (is_null($exibe['Nome1'])) { //...
  • Is this situation identical to the first correct one? if (Empty($displays['Name1'])) { //Here shows nothing

  • Watch it now 'cause it’s gonna work

  • 2

    It would be good to leave the most complete answer to the question: "Doubt about === and !=" explain why he used is_null, because he only threw one answer, but did not explain the difference and why === did not work.

  • And that’s why it’s negative? Thanks!

3

Check the PHP documentation: operators

Empty() checks if a variable is empty

isset() verifies if one or more variable(eis) (or index(s) of array) was (ram) created(o)(s) And if it has value other than NULL @Bruno Augusto

  • isset() checks whether a or more variable(eis) (or index(s) of array) was (ram) created(o)(s) And if it has a value other than NULL.

3

When you use the operator === (or its counterparty !==) you are telling the program to compare the left side with the right not only in value but also in type.

Very didactically if you have two values 0 and '0', that is, an integer zero and a numerical string, you could only distinguish them with this type of operator because they both represent the same thing, but are of different types.

Your younger brothers and sisters, == and != compare only the value. See an example:

$a = 0;
$b = '0';

var_dump( $a == $b, $a === $b );

The exit from this program is:

bool(true)
bool(false)

The first TRUE because $to has the same value as $b. The second returns FALSE because one is a integer and the other one string.

In your particular case it would only be possible to state with 100% certainty if we knew what or how the array was defined $display, however, even without knowing them it is possible to suggest that the comparison be made not only with operators, but with the functions isset() and Empty():

if ( isset( $exibe['Nome1'] ) && ! empty( $exibe['Nome1'] ) ) {}

Browser other questions tagged

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