What are best practices for updating older PHP applications to work with newer versions like PHP 5.4?

When updating older PHP applications to work with newer versions like PHP 5.4, it's important to address deprecated features, syntax changes, and potential compatibility issues. One common issue is the removal of the "mysql_" extension in PHP 5.4, which should be replaced with "mysqli_" or PDO for database interactions.

// Replace deprecated "mysql_" functions with "mysqli_" functions
$mysqli = new mysqli($host, $username, $password, $database);
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}

// Use prepared statements for secure database queries
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // Process the fetched data
}
$stmt->close();
$mysqli->close();