Why does php says unidentified variable

Why does this result in Warning: Undefined variable $coin in C:\xampp\htdocs\test\index.php on line 16

    <?php 
        $coin = 1;
        if (array_key_exists('button1', $_POST)) {
            button1();
        }
        function button1() {
            $coin += 1; //error
            echo $coin; //error
        }
    ?>

You should read this documentation regarding php variable scope which should make it clearer why $coin is undefined in the funtion.