How can the use of local variables affect the display of error and success messages in PHP scripts?

When using local variables in PHP scripts to store error or success messages, these messages may not be accessible outside of the scope of the function or block where they are defined. To ensure that error and success messages can be displayed properly, consider using global variables or passing the messages as parameters to functions that handle their display.

<?php

function displayErrorMessage($message) {
    echo "Error: " . $message;
}

function displaySuccessMessage($message) {
    echo "Success: " . $message;
}

$errorMsg = "Invalid input"; // Define error message
$successMsg = "Data saved successfully"; // Define success message

// Call functions to display messages
displayErrorMessage($errorMsg);
displaySuccessMessage($successMsg);

?>