phpsane Posted September 5, 2017 Share Posted September 5, 2017 (edited) Php Gurus, I have been doing guess work all this time. But not any more. Just gonna ask. Look at the following code: Concentrate on line 5. It is obvious the "$colors" represent the variable name of the whole array thing (or "column" or "row" in MS Excell terminology). But what does the "$value" represent ? Each array's values (or "cell" value in MS Excell terminology) ? So, the value of the variable $value changes on each loop. Right ? From "Red" to "Orange" at the end. Correct ? Example: 1st loop: $value = Red; 2nd loop: $value = Green; And so on. Correct ? <?php $colors = array("Red", "Green", "Blue", "Yellow", "Orange"); // Loop through colors array foreach($colors as $value){ echo $value . "<br>"; } ?> Edited September 5, 2017 by phpsane Quote Link to comment https://forums.phpfreaks.com/topic/304866-question-on-the-variable-array-value/ Share on other sites More sharing options...
requinix Posted September 5, 2017 Share Posted September 5, 2017 $value is an element in the array. As for what happens, run the code and see for yourself. Quote Link to comment https://forums.phpfreaks.com/topic/304866-question-on-the-variable-array-value/#findComment-1550734 Share on other sites More sharing options...
phpsane Posted September 5, 2017 Author Share Posted September 5, 2017 $value is an element in the array. As for what happens, run the code and see for yourself. Running the code showed me this: Red Green Blue Yellow Orange I figure, those values of the $value are related to the loop. Once I echo the value of the $value outside the loop then "Orange" (last value of the variable $value in the loop) is shown as the value. What do I determine from all this ? Well, I reckon the value of $value changed on each cycle and the echo inside the loop echoed each different value of the $value. The final echo that was outside the loop, only echoed the final value of the $value. <?php $colors = array("Red", "Green", "Blue", "Yellow", "Orange"); // Loop through colors array foreach($colors as $value){ echo $value . "<br>"; } echo $value . "<br>"; ?> Result: Red Green Blue Yellow Orange Orange That automatically means that the $value holds the values of each and every array. Right ? Quote Link to comment https://forums.phpfreaks.com/topic/304866-question-on-the-variable-array-value/#findComment-1550739 Share on other sites More sharing options...
ginerjm Posted September 5, 2017 Share Posted September 5, 2017 And you could have learned all of this by reading the Manual. Quote Link to comment https://forums.phpfreaks.com/topic/304866-question-on-the-variable-array-value/#findComment-1550750 Share on other sites More sharing options...
Solution requinix Posted September 5, 2017 Solution Share Posted September 5, 2017 That automatically means that the $value holds the values of each and every array. Right ?It holds each value from the $colors array, one at a time. 1 Quote Link to comment https://forums.phpfreaks.com/topic/304866-question-on-the-variable-array-value/#findComment-1550756 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.