(PHP) struggling with the variable inside a for loop

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


(PHP) struggling with the variable inside a for loop



To give some context, this is the code that the for loop refers to: putting the form data into an array.


$_SESSION['shoppingcart'][$count] = array (

'id' => filter_input(INPUT_GET, 'ID'),
'name' => filter_input(INPUT_POST, 'hidden_name'),
'price' => filter_input(INPUT_POST, 'hidden_price'),
'list_price' => filter_input(INPUT_POST, 'hidden_list_price'),
'quantity' => filter_input(INPUT_POST, 'quantity')

);

$count = count($_SESSION['shoppingcart']);

$product_ids = array_column($_SESSION['shoppingcart'], 'id');



This is all well and good: I understand what this code does. It's just the 'for' loop and everything that involves the variable '$i' outside of the for loop.


for ($i=0; $i < count($product_ids); $i++) {

if ($product_ids[$i] == filter_input(INPUT_GET, 'ID')) {

$_SESSION['shoppingcart'][$i]['quantity'] +=
filter_input(INPUT_POST,'quantity');

}
}



So, the $i variable. What does it represent in the for loop and why is it/ what does it represent in square brackets in the 'if' statement.


$i



I should say that this code accomplishes its intended purpose (putting form data into an array, incrementing the quantity if the item in the array already exists.)
I got the code from a YouTube tutorial, only he didn't explain the $i part.


quantity


$i




2 Answers
2



$i is a variable which is set to 0 by $i=0 and which has its value increased by 1 in the third part of for loop (i++). The for loop works while $i is smaller than the amount of values in $product_ids array. Therefore, if there are 20 values in $product_ids this loop will execute the code 20 times, but if you changed $i++ what is a short code of $i=$i+1 to $i = $i + 2 it would execute the code between {} 10 times and value of $i would change like this: 0, 2, 4 ... 18, 20.


$i


$i=0


i++


$i


$product_ids


$product_ids


$i++


$i=$i+1


$i = $i + 2


{}


$i



brackets in if shows that $product_ids is an array and allows to select specific value (in this code that value is $i meaning that it will change every time code executes (as $i as, I said, increases from 0 to count of values in $product_ids).



if


$product_ids


$i


$i


$product_ids



Is it clear now?



Everytime you complete an iteration of your loop the $i variable will increase by $i++ (thus in this case by 1) until this condition: $i < count($product_ids) is met. This is particularly useful for accessing elements in an array sequentially, namely if ($product_ids[$i] == filter_input(INPUT_GET, 'ID')) is used to access each element of $product_ids and check if that value is equal to filter_input(INPUT_GET, 'ID').


$i


$i++


$i < count($product_ids)


if ($product_ids[$i] == filter_input(INPUT_GET, 'ID'))


$product_ids


filter_input(INPUT_GET, 'ID')






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

415 Unsupported Media Type while sending json file over REST Template

PHP parse/syntax errors; and how to solve them?