Spialdor
Spialdor

Reputation: 1645

Php isset in html

I'm trying to do a simple isset condition in my html with php like this :

 <table cellspacing="0" cellpadding="3" border="0">
        <tr><td><?php print t('Adresse :'); ?> : </td><td><?php print $participant->adresse1; ?></td></tr>
        <tr><td><?php print t(''); ?> : </td><td><?php print $participant->code_postal.' '.$participant->ville; ?></td></tr>
        <tr><td><?php print t(''); ?> : </td><td><?php print $participant->pays; ?></td></tr>
        <?php if(isset($participant->tel_perso)): ?><tr><td><?php print t('Tél :'); ?> : </td><td><?php print $participant->tel_perso; ?></td></tr><?php endif; ?>
        <tr><td><?php print t('Email :'); ?> : </td><td><?php print $participant->email_id; ?></td></tr>
        <tr><td><?php print t('Date de naissance :'); ?> : </td><td><?php print format_date($participant->date_naissance, "custom", "d F Y") ?></td></tr>
 </table>

The problem is that this row is always show, even if the isset return false (I test it in the code before and it works)

WHy it's not working in html ?

Upvotes: 0

Views: 667

Answers (2)

Mark Tomlin
Mark Tomlin

Reputation: 8923

The problem seems to be because your value was set, but it didn't have a value that you wished to show. Without knowing what that value is, perhaps an empty call would be better for you. Displaying only the content when it's NOT ! empty. If that's not working for you, check what the variable is set to with var_dump and comment back here.

<table cellspacing="0" cellpadding="3" border="0">
    <tr>
        <td><?=t('Adresse :')?></td>
        <td><?=$participant->adresse1?></td>
    </tr>
    <tr>
        <td><?=t('')?> :</td>
        <td><?=$participant->code_postal.' '.$participant->ville?></td>
    </tr>
    <tr>
        <td><?=print t('')?> :</td>
        <td><?=$participant->pays;?></td>
    </tr>
<?php   if (!empty($participant->tel_perso))):   ?>
    <tr>
        <td><?=t('Tél :')?></td>
        <td><?=$participant->tel_perso;?></td>
    </tr>
<?php   endif;  ?>
    <tr>
        <td><?=t('Email :')?></td>
        <td><?=$participant->email_id;?></td>
    </tr>
    <tr>
        <td><?=t('Date de naissance :')?></td>
        <td><?=format_date($participant->date_naissance, "custom", "d F Y")?></td>
    </tr>
</table>

Upvotes: 2

Bert Maurau
Bert Maurau

Reputation: 979

Change your if-condition to this:

if(isset($participant->tel_perso) && $participant->tel_perso)

If yout 'tel_perso' returns false, the value is set, so you need to check for not false' as well.

Prefered way would be to use:

if(!empty($participant->tel_perso))

Like @Magnus Eriksson said, because empty() would be a valid usage here.

The following values are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)

Upvotes: 1

Related Questions