Doubt about === and! =

1

I'm having a question about showing data only if it's populated. I've already used === and != , but with none of them 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>';}

First shows. The other two exist and do not show me the data.

    
asked by anonymous 22.07.2014 / 15:35

3 answers

4

use the method is_null()

 if (is_null($exibe['Nome1'])) { //...
    
22.07.2014 / 15:39
3

Check the PHP documentation: operators

empty () checks if a variable is empty

isset () checks whether one or more variable (s) (or index (s) of array) has been created and if it has a value other than NULL @Bruno Augusto

    
22.07.2014 / 15:55
3

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

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

Now your younger siblings, == and ! = compare only the value. Here's an example:

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

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

The output of this program is:

bool(true)
bool(false)

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

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

if ( isset( $exibe['Nome1'] ) && ! empty( $exibe['Nome1'] ) ) {}
    
22.07.2014 / 16:17