So if you want to print array content using echo, you need to loop through the array and in the loop use echo to print array items. Note that because the browser condenses multiple whitespace characters (including newlines) to a single space ( answer), you need to wrap above functions in to display result in thee correct format.Īlso, there is another way to print array content with certain conditions. Code: the section on the array type for more information on what an array is. no, the dump shows that the variable is an array. In the given example, we have used the print_r() function to print all the values of the array. PHP Manual Function Reference Variable and Type Related Extensions Arrays Array Functions Change language: Submit a Pull Request Report a Bug array (PHP 4, PHP 5, PHP 7, PHP 8) array Create an array Description array ( mixed. This function prints all the values of the array along with their index value. The second parameter is the return type which is optional and by default its value is false. The print_r() function takes two arguments, the first parameter is the variable name whose value we want to print using this function. This function is basically used to print the values in more human-readable form. Without a loop, just for the kicks of it. To get the exact output you requested, you could encode the array as JSON, e.g. We can also print all the values of the array using the print_r() function. In the given example, we have used the foreach loop to print all the elements of an array. You will need to use a loop to iterate through each element of the array and print it out. However, you can't simply use echo to display the contents of an array as it is. It is the easiest method to print all the array elements at once. In PHP, you can use the echo statement to output arrays. We can use the PHP count() or sizeof() function to get the particular number of elements or values in an array. We will get an error if we try to use the foreach loop with variables or other data types. 14 Answers Sorted by: 588 To see the contents of array you can use: printr (array) or if you want nicely formatted array then: echo '' printr (array) echo '' Use vardump (array) to get more information of the content in the array like the datatype and length. The foreach loop works on an array and objects only. The foreach loop iterates through each element within the array. We can print all the values of the array using a PHP foreach loop.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |