If I use the following line of code in a couple places in the same script, will it cause “collisions”…
foreach($commentsArray as $commentKey => $subArr){
}
In other words, what is the scope of $commentKey and $subArr ??
Sincerely,
Debbie
If I use the following line of code in a couple places in the same script, will it cause “collisions”…
foreach($commentsArray as $commentKey => $subArr){
}
In other words, what is the scope of $commentKey and $subArr ??
Sincerely,
Debbie
No it won’t, the variables assigned in the loop are scoped only to it and are released once the loop is finished or manually broken using break;
Glad to hear! (That will be easier for me to manage instead of having to try and come up with unique names every time I need to access my array!)
Thanks for the quick response!
Sincerely,
Debbie
You must be thinking of C or Java or something like that. In PHP, they stay alive.
$commentsArray = ['a' => 'b'];
foreach($commentsArray as $commentKey => $subArr){
}
echo $commentKey; // a
echo $subArr; // b
Yes just like in a WHILE loop, if you set a variable inside the loop, the last value for that variable is still set.
As mentioned, it is available outside of the loop. If the loop is in a function, the variable’s scope is that function; etc
Normally when I populate a web page I just access the data directly from my Prepared Statement using a loop.
But my current script is quite a bit more complicated, so I stuck all of the data in a multi-dimensional array.
I need to access the data in the array twice: First to display it, and second to process my $_POST.
Should I have different variable names to be safe, or since I will be iterating through the entire array in each case, can I assume it is okay to just use the same variable names as implied in my OP?
Sincerely,
Debbie
P.S. Thanks for catching the error in the advice above!! :tup:
It seems like it would be fine to use it as is for both purposes, technically speaking (and without seeing what’s going on in the code), but I would feel more comfortable first processing the POST, and then running a second query to fetch the comments. That way you can avoid displaying the comment, only to have it rejected by the DB for whatever reason
This is script is about 2,000 lines of code, so I can’t post it here, but the gist of it is this…
First I query the database for Comments, stick them in my array, and then drop down to Form and display them using my ForEach loop.
Then when someone rates one of the Comments, I have a $_POST request which necessitates that I loop through the array a second time, but this time potentially building an Error Message for the single Comment which was rated.
So in both cases, I always need to iterate through all Comments. It is just in the first case it is to build the Comments themselves, and the second For Each is to find the Comment which was voted on, and build an Error Message, if that makes sense?!
I suppose to be safe I could create slightly different variable names, but I think technically using the same ones would be okay.
Sincerely,
Debbie
Oh, okay, no it’s fine, it’s actually better to reuse it without changing anything in that case
I could always initialize things at the top of my script, too, like this…
$commentKey = '';
$subArr = '';
Debbie
You could, but there really is no need. It would only add clutter to the script.