How can you display array values in PHP?
In PHP, you can display the values of an array using several methods, depending on the context. Below are some of the most common ways to display array values:
1. Using implode
:
PHP
<?php
$cars = array('1. Volkswagen', '2. Toyota', '3. Mercedes', '4. Ford', '5. BMW');
echo implode(', <br />', $cars);
?>
Output:
1. Volkswagen,
2. Toyota,
3. Mercedes,
4. Ford,
5. BMW
2. Toyota,
3. Mercedes,
4. Ford,
5. BMW
2. Using foreach Loop
PHP
<?php
$array = ['1. Volkswagen', '2. Toyota', '3. Mercedes', '4. Ford', '5. BMW'];
foreach ($array as $value) {
echo $value . "<br>";
}
?>
3. Accessing Associative Array Values
PHP
$array = [
"First Name" => "Jane",
"Last Name" => "Doe",
"Age" => 45,
"City" => "Amsterdam"
];
foreach ($array as $key => $value) {
echo $key . ": " . $value . "<br>";
}
Output:
First Name: Jane
Last Name: Doe
Age: 45
City: Amsterdam
Last Name: Doe
Age: 45
City: Amsterdam
Arithmetic operators
Background that adapts to the current season
Calculate the similarity of text
Comparison operators
Count page views in a session
Display a random image
Display a random text
Display array values
Display a time based greeting
Display the current copyright year
Display current date
Display the current date in another language
Display date based on system language settings
Quiz with input fields
Quiz with input fields, multi-steps
Quiz with radio buttons
Quiz with radio buttons, multi-steps
Redirect users to another page
Retrieve checkbox values
Retrieve form input values
Retrieve the value of the selected option
Retrieve the value of the star rating
Store the value of a radio button in a session
Truncate a string to a specified width
Word Wrap