clas.php

<?php
session_start();

class mathcaptcha
{
    private $bil1;
    private $bil2;
    private $operator;

    function initial()
    {
        $listoperator = array('+', '-', 'x');
        $this->bil1 = rand(0, 99);
        $this->bil2 = rand(0, 99);
        $this->operator = $listoperator[rand(0, 2)];
    }

    function generatekode()
    {
        $this->initial();

        if ($this->operator == '+') $hasil = $this->bil1 + $this->bil2;
        else if ($this->operator == '-') $hasil = $this->bil1 - $this->bil2;
        else if ($this->operator == 'x') $hasil = $this->bil1 * $this->bil2;
        $_SESSION['kode'] = $hasil;
    }

    function showcaptcha()
    {
        echo "Berapa hasil dari ".$this->bil1." ".$this->operator." ".$this->bil2." ? ";
    }	

    function resultcaptcha()
    {
        return $_SESSION['kode'];
    }

}
?>

index.php
<?php
    // memanggil script class
    include 'class-captcha.php';
    // membuat obyek class
    $captcha1 = new mathcaptcha();
    // panggil method untuk mengenerate kode
    $captcha1->generatekode();
?>

<html>
    <head>
       <title>Contoh Captcha Matematika</title>
    </head>
    <body>
        <h1>Form Entri Data</h1>
        <form method="post" action="submit.php">
            <table>
                <tr><td>Nama Anda</td><td>:</td><td><input type="text" name="nama"></td></tr>
                <tr><td>Email Anda</td><td>:</td><td><input type="text" name="email"></td></tr>
            </table>	

            <p><b>Kode Verifikasi</b></p>
            <p>
              <?php
                   // menampilkan kode captcha berisi soal matematika
                   $captcha1->showcaptcha();
              ?>
            <br>
            <input type="text" name="kode">
            </p>
            <p><input type="submit" name="submit" value="Submit"></p>

        </form>
    </body>
</html>

Source : blog.rosihanari.net