RE: ternary operator sintax help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thanks. I will give up on <?= then. And use echo instead.
 
I have declared the erros variable like this:
 
$erros=array();
 
Instead of isset I’ve also tried with empty();
 
If I have something like this, it works:
 
<div class="mensagemErro"><?php echo (isset($erros['anexo'])) ? $erros['anexo'] : ''; ?></div>
 
I mean, it will show the message only when the form gets submitted. At the beginning it will have ‘’. 
 
:s
 
Regards,
Marcio
 
 
 
From: Ashley Sheridan [mailto:ash@xxxxxxxxxxxxxxxxxxxx] 
Sent: terça-feira, 6 de Outubro de 2009 19:49
To: MEM
Cc: php-general@xxxxxxxxxxxxx
Subject: Re:  ternary operator sintax help
 
On Tue, 2009-10-06 at 19:43 +0100, MEM wrote: 
 
Hello all,
 
I'm trying to display a div, only when some php value is set.
Since this will be near html, I'd like to keep it on one line. So, I'd love
to use shortcuts and a ternary operator for the effect.
 
I'm having something like this right now, but the div still appears even if
the error is NOT set.
 
<?= (isset($erros['anexo'])) ? '<div class="mensagemErro">'
.$erros['anexo'].'</div>' :''; ?>
 
:( 
 
Can I have your help with the right syntax ?
 
 
Regards,
The newbie on a Humpty Dumpty wall,
Márcio
 
 

Firstly, <?= is a bad idea, as it's not very portable and relies on short tags being on, which causes all sorts of problems should you ever need PHP to output XML.

The only reason I can see that the div will always appear is a logic error. Are you sure that the $erros['anexo'] variable is not set at some point and not just set to an empty string? An empty string is very different from an unset variable. Try using var_dump($erros['anexo']); in your code somewhere to see if the it is actually an empty string instead.

Thanks,
Ash
http://www.ashleysheridan.co.uk


 

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux