How can a PHP developer ensure proper separation of concerns when displaying a message and redirecting to another page?
To ensure proper separation of concerns when displaying a message and redirecting to another page in PHP, a developer can use the concept of sessions to store the message and then redirect to the desired page. By separating the logic for displaying messages and redirecting into different parts of the code, the concerns are properly isolated.
```php
// Display a message
session_start();
$_SESSION['message'] = 'This is a success message.';
header('Location: index.php');
exit();
```
In the above code snippet, we first start a session and store the message in the session variable. Then, we use the header function to redirect to the index.php page. This way, the message is separated from the redirect logic, ensuring proper separation of concerns.
Related Questions
- What are best practices for checking if a specific index exists in an array in PHP?
- How can PHP version compatibility affect the implementation of a script to modify RDF content?
- In what scenarios would using the rename function or move_uploaded_file function be more appropriate than fopen for writing a string to a .txt file on an FTP server using PHP?