Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

foreach variables

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
jonathan978123
Smarty n00b


Joined: 03 Mar 2021
Posts: 3

PostPosted: Wed Mar 03, 2021 8:52 pm    Post subject: foreach variables Reply with quote

Hi,

I recently discovered that variables within smarty loops do not seem to work the same as within PHP.

Smarty code:

Code:

{$ary = [[1], [2], [3]]}
{$innerArray = 'test'}

{foreach $ary as $innerArray}
    Inside loop: {$x = print_r($innerArray)}<br>
{/foreach}

<br>
Outside loop:
{$x = print_r($innerArray)}

Output:
Code:

Inside loop: Array ( [0] => 1 )
Inside loop: Array ( [0] => 2 )
Inside loop: Array ( [0] => 3 )

Outside loop: test


Is this the intended behavior?
I had expected smarty to behave like PHP: with "Outside loop: Array ( [0] => 3 )"

I checked the documentation, but didn't see anything about it.

Can someone explain why it behaves like this?
Thanks Smile
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1723

PostPosted: Thu Mar 04, 2021 12:01 pm    Post subject: Reply with quote

Yes, this is an intended behavior.

1. Internally, Smarty do not use provided variable names in compiled code.
2. Even in PHP, the state of internal variable after the loop is undefined.
Back to top
View user's profile Send private message
jonathan978123
Smarty n00b


Joined: 03 Mar 2021
Posts: 3

PostPosted: Thu Mar 04, 2021 8:07 pm    Post subject: Reply with quote

Hi AnrDaemon,

Thanks for your reply.

I'm quite sure that your second point is incorrect.
If I run the following PHP code:
Code:

<?php
$ary = [[1], [2], [3]];
$innerArray = 'test';
foreach ($ary as $innerArray)
{
    echo "Inside loop: " . print_r($innerArray, true) . "<br>";
}
echo "<br>Outside loop: " . print_r($innerArray, true);

exit;


The output is as I expect:
Code:

Inside loop: Array ( [0] => 1 )
Inside loop: Array ( [0] => 2 )
Inside loop: Array ( [0] => 3 )

Outside loop: Array ( [0] => 3 )


(in this case assigning $innerArray outside the loop is useless because we can know for sure that it will be overwritten, but that's besides the point...)

Or are you talking about something else?
Thanks!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1723

PostPosted: Fri Mar 05, 2021 4:12 pm    Post subject: Reply with quote

The wording of https://www.php.net/foreach was changed, but it is still suggesting that using foreach's inner variables outside the loop is prone to erroneous behavior.
Back to top
View user's profile Send private message
jonathan978123
Smarty n00b


Joined: 03 Mar 2021
Posts: 3

PostPosted: Tue Mar 09, 2021 8:21 pm    Post subject: Reply with quote

Thanks for the link.

I see my perception of foreach was not fully correct.
I will use end() https://www.php.net/manual/en/function.end.php to grab the last element in an array outside of the loop.

Thanks again!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General 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