Why is it advisable to incorporate security measures and best practices during the initial development phase of PHP scripts rather than retroactively?

It is advisable to incorporate security measures and best practices during the initial development phase of PHP scripts because it is more efficient and cost-effective to address security vulnerabilities from the beginning rather than trying to fix them retroactively. By implementing security measures early on, you can prevent potential security breaches and protect sensitive data.

// Example of incorporating security measures in PHP script during initial development phase
// Use parameterized queries to prevent SQL injection

// Establish database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Prepare and bind SQL statement with parameters
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

// Set parameters and execute
$username = "admin";
$stmt->execute();

// Process results
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    echo "Username: " . $row["username"] . "<br>";
}

// Close statement and connection
$stmt->close();
$conn->close();