Re: [SMARTY] Int incr/decr on the fly

From: Date: Mon, 19 Jan 2004 14:10:12 +0000
Subject: Re: [SMARTY] Int incr/decr on the fly
References: 1 2 3  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-10854@lists.php.net to get a copy of this message
Luca Borrione wrote:
{tag var=$myvar++} Does the trick. I know I can avoid this using $myvar+1 as described in the previous link, but, only because I'm used to this, it would be nice to use this syntax. Unluckly I cannot.. that's my experience with 2.6.0
    {assign var="foo" value=1}
    {assign var="test" value=$foo++}
      or
    {assign var="test" value="$foo++"}
      or
    {assign var="test" value=$foo++}
    {$test}
    OUTPUT: 1
    {assign var="foo" value=1}
    {assign var="test" value="$foo++"}
    OUTPUT: 1++
I cannot output 2! So, what d'you mean with *does the trick*?
I think you may be a bit confused about the ++ operator or the quoting of smarty vars. In normal php, ++ can be used as a prefix or postfix unary operator, eg: ++$myvar - increments $myvar before it is evaluated. (AFAIK smarty doesn't allow this) $myvar++ - will evaluate the expression using the current value of $myvar, and then increment it afterwards. see: http://www.php.net/manual/en/language.operators.increment.php Example code: {assign var="foo" value=1} {assign var="bar" value=$foo++} foo: {$foo} bar: {$bar} results: foo: 2 bar: 1 In the above example: $foo is assign the value of 1. $bar is assigned the value of $foo (ie. 1) and then $foo is incremented. You also need to remove the quotes, ie: {tag var=$myvar++} or use backticks: {tag var="$myvar++"} see: http://smarty.php.net/manual/en/language.syntax.quotes.php -- Mark Gibson <gibsonm@cromwell.co.uk> Web Developer & Database Admin Cromwell Tools Ltd. Leicester, England.

« previous php.smarty.general (#10854) next »