Re: [SMARTY] String equality also matching numeric 0 with key
| From: | messju mohr | Date: | Mon, 09 Feb 2004 20:13:53 +0000 |
| Subject: | Re: [SMARTY] String equality also matching numeric 0 with key | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-10936@lists.php.net to get a copy of this message | ||
On Mon, Feb 09, 2004 at 02:44:36PM -0500, Chris Shenton wrote:
> I'm trying to process results of a PHP LDAP query where one element of
> the result is an array, the data is in numeric-indexed elements and
> the count of the elements has the index 'count'.
>
> When I try to use Smarty to check for this 'count' key using
>
> {if $var == "count"}
>
> it catches that key name, but ALSO catches the index=0 key.
that's php's funny way of typecasting.
0 == "count" => true
"0" == "count" => false
0 === "count" => false
(smarty maps the eq-operator to a "==").
> What am I missing, why's the equality match not working as documented
> for "Fred" and "Wilma" at
> http://smarty.php.net/manual/en/language.function.if.php
only strings are used in these examples so they work.
> Here's some short test code, template, and output showing this.
> Smarty is version 2.6.1.
>
> Thanks.
>
>
> PHP -----------------------------------------------------------------
>
> <?php
> $foo = array('count' => 3,
> '0' => 'zero@example.com',
> '1' => 'one@example.com',
> '2' => 'two@example.com');
>
> echo "<PRE>" . print_r($foo, true) . "</PRE>";
>
> require_once('Smarty.class.php');
> $smarty = new Smarty;
> $smarty->assign('foo', $foo);
> $smarty->display('foreachtest.tpl');
> ?>
>
> TEMPLATE ------------------------------------------------------------
>
> {foreach item=i key=k from=$foo}
> {if $k eq "count"}
{if $k === "count"}
should work for you.
> k={$k} i={$i}<br>
> {/if}
> {/foreach}
>
>
> GENERATED PAGE ------------------------------------------------------
>
> Array
> (
> [count] => 3
> [0] => zero@example.com
> [1] => one@example.com
> [2] => two@example.com
> )
>
> k=count i=3
> k=0 i=zero@example.com