|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
jonathan978123 Smarty n00b
Joined: 03 Mar 2021 Posts: 4
|
Posted: Wed Mar 03, 2021 8:52 pm Post subject: foreach variables |
|
|
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 |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Mar 04, 2021 12:01 pm Post subject: |
|
|
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 |
|
jonathan978123 Smarty n00b
Joined: 03 Mar 2021 Posts: 4
|
Posted: Thu Mar 04, 2021 8:07 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Mar 05, 2021 4:12 pm Post subject: |
|
|
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 |
|
jonathan978123 Smarty n00b
Joined: 03 Mar 2021 Posts: 4
|
Posted: Tue Mar 09, 2021 8:21 pm Post subject: |
|
|
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 |
|
|
|
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
|