How can you return an array from a function in PHP?
To return an array from a function in PHP, you can simply create an array within the function and return it using the `return` keyword. You can populate the array with values or manipulate it as needed within the function before returning it.
function getArray() {
$myArray = [1, 2, 3, 4, 5];
return $myArray;
}
// Call the function and store the returned array
$arrayResult = getArray();
// Output the returned array
print_r($arrayResult);
Keywords
Related Questions
- What are the best practices for implementing a timeout feature to mark a user as offline in a PHP chat application?
- What potential pitfalls should be considered when sending XFDF files via email using PHP?
- How can PHP developers ensure a No-JavaScript-Fallback option for users who have disabled JavaScript in their browsers?