0

Is the following acceptable, even if $my_flag may not be set?

{if ( $my_flag )}
    // Vrooom
{/if}

Do I always need isset to ensure notices aren't thrown, or does Smarty3 take care of things?

TheDeadMedic
  • 9,948
  • 2
  • 35
  • 50
  • Yes, and all seems well. I'm more concerned that when the template is compiled, I want to be sure the corresponding PHP won't throw errors with `E_STRICT` – TheDeadMedic Jul 16 '12 at 10:39

1 Answers1

0

It is acceptable, but PHP will warn you that $my_flag is undefined.

I would do something like that:

{if isset($my_flag)}
    {if $my_flag == 123}
        <p>print something</p>
    {/if}
{/if}

If you will try your current script everything will work, but it will log a notice. The above script makes sure the only if the variable is set then do something.

However, since Smarty is cached you may have to reload the page a few times to see the changes (unless you specify $smarty_obj->force_compile = true;).

Maerlyn
  • 33,687
  • 18
  • 94
  • 85
Nadav S.
  • 2,429
  • 2
  • 24
  • 38