Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

key and "if" comparison doesn't work [solved]

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
olivierpons
Smarty Rookie


Joined: 13 Aug 2011
Posts: 13
Location: France

PostPosted: Sun Oct 09, 2011 3:19 pm    Post subject: key and "if" comparison doesn't work [solved] Reply with quote

Hi,

I pass an array to smarty.
The dump of this array is:

Code:
tab_authentification_ok = array (
  'url_redirection' => '/intranet/',
  0 => 'Bienvenue, Olivier Dofus',
  1 => 'Veuillez patientez, vous allez être redirigé vers l\'intranet',
)


Here's my very simple loop in my smarty template:

Code:
{foreach key=key item=item from=$tab_authentification_ok}
  {if $key neq 'url_redirection'}
  <p>OK =&gt; {$item}</p>
  {else}
  <h1>Key should be 'url_redirection' =&gt; {$key}</h1>
  {/if}
{/foreach}


The previous code should give:
Key should be 'url_redirection' => url_redirection
OK => Bienvenue, Olivier Dofus
OK => Veuillez patientez, vous allez être redirigé vers l'intranet

But it gives:

Key should be 'url_redirection' => url_redirection
Key should be 'url_redirection' => 0
OK => Veuillez patientez, vous allez être redirigé vers l'intranet

Am I missing something?
What am I doing wrong?


Last edited by olivierpons on Sun Oct 09, 2011 3:25 pm; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
olivierpons
Smarty Rookie


Joined: 13 Aug 2011
Posts: 13
Location: France

PostPosted: Sun Oct 09, 2011 3:24 pm    Post subject: Reply with quote

Damn I found it, but it's not logical to me. Here's the solution: ternary comparison (!==) instead of "neq".

This is not normal I think.

Code:
  {if $key !== 'url_redirection'}
  <p>OK =&gt; {$item}</p>
  {else}
  <h1>Key should be 'url_redirection' =&gt; {$key}</h1>
  {/if}
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Oct 09, 2011 3:39 pm    Post subject: Reply with quote

It is a PHP behavior between string and numeric comparison. Try the exact same thing in PHP code. (!= vs !==) Also !== is a comparison operator, not ternary.

Code:
// compare int to string
 if(0 != 'foo') { echo "ok"; } else { echo "what?"; }
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP