How effective are traditional captchas in preventing spam from bots in PHP forms?

Traditional captchas can be effective in preventing spam from bots in PHP forms by requiring users to complete a challenge that is easy for humans but difficult for bots to solve. This can help verify that the form submission is coming from a real person and not an automated script.

<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST["captcha"]) && $_POST["captcha"] == $_SESSION["captcha"]) {
        // Captcha verification successful, process form submission
    } else {
        // Captcha verification failed, display error message
        echo "Captcha verification failed. Please try again.";
    }
}

// Generate random captcha code and store it in session
$captcha = substr(md5(rand()), 0, 6);
$_SESSION["captcha"] = $captcha;
?>

<form method="post" action="">
    <label for="captcha">Enter the code shown below:</label><br>
    <img src="captcha_image.php" alt="Captcha Image"><br>
    <input type="text" id="captcha" name="captcha"><br>
    <button type="submit">Submit</button>
</form>