![]() For example, the data type, index number, length, and value.Įxample 5.1: This example shows PHP how to print an array using the var_dump() function. The var_dump() prints some additional information about array elements. In PHP, we use the var_dump() function to dump information about one or more arrays or variables in a structured format. ![]() Now, let’s go ahead to our next PHP method to print an array. The same method is used to print multi-dimensional arrays. 24, "john" => 28, "rocky" => 30, "scarlett" => 26 ) // print associative array using using print_r() function echo "" print_r ( $students ) echo "" ?> Code Output: The same PHP method applies to print associative arrays.Įxample 4.3: This example shows how to print associative arrays using the print_r() function in PHP. The above output is written clearly on separate lines and is readable. " print_r ( $students ) echo "" ?> Code Output: This will print array content on separate lines instead of a single line. We used pair of tags using an echo statement. We can also use HTML tags, to make it more readable, like in the given example below.Įxample 4.2: In the following code, just see lines no. If you see the output of the code, the array is displayed in a structured format with values and index numbers on a single line. ![]() In the above code, I used the print_r() function with the array name ( $students) as a parameter. Advertisements Code Output:Īrray ( => mike => john => rocky => scarlett ) It is TRUE and FALSE.Įxample 4.1: This example shows how to use the print_r() function to print an array. The print_r() has two parameters but the second parameter is optional. The syntax is very simple, we use the array name as a parameter inside this function. This function print PHP arrays in a structured format. The print_r() function is mostly used for testing purposes. But when the array is too long, then we use loops and other methods to display it. In that case echo statement really helps. Sometimes, we need to display array values in different ways like inside HTML tables. The above code output will look like this in your browser: Code Output: We can access each element of an array using its index number. Remember, the index number of an array starts from 0. And also, I added a tag to display each value of an array on a new line. I used an echo statement with an array name and index number on each line. Now, let’s see the PHP code to print an array using echo statements.Įxample 3.1: This example will show you how to use an echo statement to print all the values of an array one by one. While in terms of arrays, echo is not the only solution but it is still helpful in many cases.įor example, echo helps a lot when we want to print a single value of an array. In PHP, whenever we want to print something, we use echo statements. The echo statement is useful in some cases, now let’s see that in the next method. But there is an echo statement that we use mostly to print a single array element. We use these variables inside the loop to print the keys and values of an associative array.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |