Question:
PHP: Is output of foreach a string or array?
2013-01-27 18:37:14 UTC
I know that input of foreach needs to be an array, but does it output a string or array?
Four answers:
Brian K
2013-01-28 10:04:41 UTC
If you are refering to the $value in foreach ($array as $value) or foreach ($array as $key => $value), array elements may be any PHP type (boolean, integer, string, array, object, NULL, etc.) in any combination and order. If $array is not empty, the $value at completion of the foreach will be the last value in $array (unless modified within the body of the foreach).



However, the term "output" usually refers to something that is printed (echoed) or sometimes a returned value (either pass-by-reference or return) from a function. As a control statement, I would be surprised if many programmers referred to assignments to $value as the foreach's "output", as opposed to something like:



foreach ($array as $value) {

echo "I would call these echoes of $value the foreach's 'output'";

}



# A loop used in calculating a result without producing any "output"

$total = 0;

foreach ($amounts as $amount) { $total += $amount; }
2013-01-27 18:41:05 UTC
It outputs each element on each iteration. For example:



$array = array(0 => "First", 1 => "Second", 2 => "Third");



foreach($array as $key => $value)



For the first iteration, $key will be 0 and $value will be "First" and on the second $key will be 1 and $value will be "Second" and so on.
finto
2017-01-12 01:42:20 UTC
would not the database act as a placeholder on your array? won't be able to you in simple terms cycle by your array and shop each and each get entry to as a string? Then, once you opt for the files lower back you examine the column and shop the entries as an array.
John
2013-01-27 18:39:43 UTC
IoC via DI and a DI container causes loosely coupled class design


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...